Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Error handling in JavaScript

Suggested Videos
Part 28 - JavaScript closure example
Part 29 - JavaScript arguments object
Part 30 - Recursive function in JavaScript



Use try/catch/finally to handle runtime errors in JavaScript. These runtime errors are called exceptions. An exception can occur for a variety of reasons. For example, referencing a variable or a method that is not defined can cause an exception.



The JavaScript statements that can possibly cause exceptions should be wrapped inside a try block. When a specific line in the try block causes an exception, the control is immediately transferred to the catch block skipping the rest of the code in the try block.

JavaScript try catch example :
try
{
    // Referencing a function that does not exist cause an exception
    document.write(sayHello());
    // Since the above line causes an exception, the following line will not be executed
    document.write("This line will not be executed");
}
// When an exception occurs, the control is transferred to the catch block
catch (e)
{
    document.write("Description = " + e.description + "<br/>");
    document.write("Message = " + e.message + "<br/>");
    document.write("Stack = " + e.stack + "<br/><br/>");
}

document.write("This line will be executed");

Output :
JavaScript try catch example

Please note : A try block should be followed by a catch block or finally block or both.

finally block is guaranteed to execute irrespective of whether there is an exception or not. It is generally used to clean and free resources that the script was holding onto during the program execution. For example, if your script in the try block has opened a file for processing, and if there is an exception, the finally block can be used to close the file.

JavaScript try catch finally example :
try
{
    // Referencing a function that does not exist cause an exception
    document.write(sayHello());
    // Since the above line causes an exception, the following line will not be executed
    document.write("This line will not be executed");
}
// When an exception occurs, the control is transferred to the catch block
catch (e)
{
    document.write("Description = " + e.description + "<br/>");
    document.write("Message = " + e.message + "<br/>");
    document.write("Stack = " + e.stack + "<br/><br/>");
}
finally
{
    document.write("This line is guaranteed to execute");
}

Output : 
JavaScript try catch finally example

Syntax errors and exceptions in JavaScript
try/catch/finally block can catch exceptions but not syntax errors.

Example : In the example, below we have a syntax error - missing the closing parentheses. The associated catch block will not catch the syntax errors.
try
{
    alert("Hello";
}
catch (e)
{
    document.write("JavaScript syntax errors cannot be caught in the catch block");
}

JavaScript throw statement : Use the throw statement to raise a customized exceptions. 

JavaScript throw exception example :

function divide()
{
    var numerator = Number(prompt("Enter numerator"));
    var denominator = Number(prompt("Enter denominator"));

    try
    {
        if (denominator == 0)
        {
            throw {
                error: "Divide by zero error",
                message: "Denominator cannot be zero"
            };
        }
        else
        {
            alert("Result = " + (numerator / denominator));
        }
    }
    catch (e)
    {
        document.write(e.error + "<br/>");
        document.write(e.message + "<br/>");
    }
}

divide();

JavaScript tutorial

No comments:

Post a Comment

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.