try...catch...finally Statement (JavaScript)

Sets up blocks of code in which errors that are thrown in one block are handled in another. Errors that are thrown inside the try block are caught in the catch block. JavaScript.

try {
    tryStatements
}
catch(exception){ 
    catchStatements
}
finally {
    finallyStatements
}
tryStatements

Required. Statements where an error can occur.

exception

Required. Any variable name. The initial value of exception is the value of the thrown error.

catchStatements

Optional. Statements to handle errors occurring in the associated tryStatements.

finallyStatements

Optional. Statements that are unconditionally executed after all other error processing has occurred.

The try...catch...finally statement provides a way to handle some or all of the errors that may occur in a given block of code, while still running code. If errors occur that are not handled, JavaScript provides the normal error message.

The try block contains code that may provoke an error, while the catch block contains the code that handles some or all errors. If an error occurs in the try block, program control is passed to the catch block. The value of exception is the value of the error that occurred in the try block. If no error occurs, the code in the catch block is never executed.

You can pass the error up to the next level by using the throw statement to re-throw the error.

After all the statements in the try block have been executed and error handling has been done in the catch block, the statements in the finally block are executed, whether or not an error was handled. The code in the finally block is guaranteed to run unless an unhandled error occurs (for example, a run-time error inside the catch block).

The following example causes a ReferenceError exception to be thrown and displays the name of the error and its message.

try {
    addalert("bad call");
}
catch(e) {
    document.write ("Error Message: " + e.message);
    document.write ("<br />");
    document.write ("Error Code: ");
    document.write (e.number & 0xFFFF);
    document.write ("<br />");
    document.write ("Error Name: " + e.name);
}

// Output:
Error Message: 'addalert' is undefined
Error Code: 5009
Error Name: ReferenceError

The following example shows how to re-throw errors, as well as the execution of nested try…catch blocks. When the error is thrown from the nested try block, it passes to the nested catch block, which re-throws it. The nested finally block runs before the outer catch block handles the error, and at the end the outer finally block runs.

try {
    document.write("Outer try running...<br/>");

    try {
        document.write("Nested try running...<br/>");
        throw new Error(301, "an error");
    }
    catch (e) {
        document.write ("Nested catch caught " + e.message + "<br/>");
        throw e;
    }
    finally {
        document.write ("Nested finally is running...<br/>");
    }
}
catch (e) {
    document.write ("Outer catch caught " + e.message + "<br/>");
}
finally {
    document.write ("Outer finally running");
}

// Output:
// Outer try running...
// Nested try running...
// Nested catch caught error from nested try
// Nested finally is running...
// Outer catch caught error from nested try
// Outer finally running

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

Show:
© 2014 Microsoft. All rights reserved.