blog

Home / DeveloperSection / Blogs / JavaScript Errors & Exception Handling

JavaScript Errors & Exception Handling

AVADHESH PATEL4402 19-Nov-2012
Introduction:

In this article I am going to explain the types of errors and what feature JavaScript had provided us to handle errors. 

There are basically three types of Errors:

1.    Syntax Error: It occurs at interpret time. The reason behind this error is the syntax of the language which is not followed correctly like there must be a parenthesis which must be in a function but we do not have use it.

Then syntax error will occur.

2.    Runtime Errors: This error occurs at runtime after the compilation/Interpretation of code. This error occurs   if we have not defined a variable but trying to access it.

3.   Logical Errors: These types of errors are most difficult to find out. If this error occurs then our program will  run but it will not give correct output.

For handling these types of errors JavaScript had provided us error handling techniques which are discussed below.

Try….catch….finally statement

JavaScript Try Catch statement is used to check a block of code for errors. The try block contains the code which

needs to be checked for errors. Catch block contains code to be executed if any errors occurs and finally is that block

of code which will execute whether try or catch block had executed or not.

Syntax:

try{
//code to be checked for.
}
catch(e)
{
//code which will be executed if any error occurs.
}
finally
{
//code which will execute whether catch block has executed or not.
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script type="text/javascript">
        try {
            namelist();
            alert("Function Exist");
        }
        catch (e) {
            document.write("Error: " + e.Message + "<br/>");
        }
    </script>
</body>
</html>

Output:
Error: undefined

 In this example if name list function does not exist in the above script then

try block will give the control to the catch block and catch block will

handle the code accordingly and print the error message.

Example of finally statement used in conjunction with the try and catch


statement:


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <script type="text/javascript">
        try {
            namelist();
            alert("Function Exist");
        }
        catch (e) {
            document.write("Error: " + e.Message + "<br/>");
        }
        finally {
            document.write("This is a finally block, this will execute irrespective of the other block");
        }
    </script>
</body>
</html>
Output:

 Error: undefined

This is a finally block, this will execute irrespective of the other block

throw statement:

Throw statement is used to raise our own customized exception. It allows us to create our own exception.

Syntax:

throw exception

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript">
        var number = prompt("Enter number greater than zero and less than 100", "");
        try {
            if (number < 0) {
                throw "Error1";
            }
            else if (number >= 100) {
                throw "Error2";
            }
        }
        catch (err) {
            if (err == "Error1") {
                alert("Error! Please enter number greater than 1 ");
            }
            if (err == "Error2") {
                alert("Error! Please enter number less than 100 ");
            }
        }
    </script>
</body>
</html>

 

Output:

JavaScript Errors & Exception Handling


In this example I have shown how we can create our customized exception.

Conclusion:

In this article I have explained about error handling techniques in


JavaScript which is try and catch. I have


created my own customized error handling exception.


Updated 18-Sep-2014
Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)

Leave Comment

Comments

Liked By