Home > DeveloperSection > Articles > Exception handling in Java Script

Exception handling in Java Script


JavaScript JavaScript 
Ratings:
0 Comment(s)
 6352  View(s)
Rate this:

Exception handling in Java Script

A runtime error which causes to break the execution of a program is known as exception and mechanism to handle this exception so that program did not terminate abnormally is known as Exception handling. In case of java script when web pages are browses through explorer over the internet, sometimes we seen a special message which tells that there is a runtime error in page and do you want to debug this page. This message comes because an exception is generating in your web page and browser told you about that exception. Error messages like this may be helpful for developers but might not good for users. When users see this type of error they normally leave the message. In this demonstrator we learn that how to handle exception I n java script by using try catch block.

We can use try…catch block to handle runtime errors known as exception. We can put java script statements inside try block which have possibility to raise an exception and perform required action in catch block when any exception is generated. Following code snippet give you a demonstration to use try catch block. When user run this application then a button is displayed and when user click on that button then an alert box is open and displaying an exception.

Code:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

    <script type="text/javascript">

        /*

            This function will generate an exception because

            alert is missspelled.

        */

        function displayMessage() {

            try {

                alertd("Hello World");

            }

            catch (err) {

                var text = "An error is ocuured in this page.\n\n";

                text += "Description of error is  :" + err.Description;

                text += "\n\nClick OK button to continew.";

                alert(text);

            }

        }

    </script>

</head>

<body>

    <input type="button" value="Click this to view message" onclick="displayMessage()" />

</body>

</html>

Output of the following code snippet is as follows

Exception handling in Java Script

Exception handling in Java Script

Using Throws statement in Exception handling

We can use throw statement to control flow of exception handling. In simple we can say when we want to create user defined exception handling then we can use throw statement. With the help of throw statement we can control flow exception and generate an accurate message according to exception. The following example demonstrates use of exception handling by using throw statement.

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

    <script type="text/javascript">

        /*

            This function will generate an exception because

            alert is missspelled.

        */

        function displayMessage() {

            var empAge = 17;

            try {

                if (empAge < 0) {

                    throw "err1";

                }

                if (empAge < 18) {

                    throw "err2";

                }

                if (empAge > 50) {

                    throw "err3";

                }

            }

            catch (err) {

                if (err == "err1")

                    alert("Invalid age");

                if (err == "err2")

                    alert("Age should be greater than or equal to 18.");

                if (err == "err3")

                    alert("You are overage.");

            }

        }

    </script>

</head>

<body>

    <input type="button" value="Click this to view message" onclick="displayMessage()" />

</body>

</html>

Output of the following code snippet is as follows

Exception handling in Java Script

Exception handling in Java Script


Don't want to miss updates? Please click the below button!

Follow MindStick