try...catch...finally Statement

Implements error handling for JScript.

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

Arguments

  • tryStatements
    Optional. 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.

Remarks

The try...catch...finally statement provides a way to handle some or all of the possible errors that may occur in a given block of code, while still running code. If errors occur that the programmer has not handled, JScript simply provides its normal error message to a user, as if there was no error handling.

The tryStatements contain code where an error can occur, while catchStatements contain the code to handle any error that does occur. If an error occurs in the tryStatements, program control is passed to catchStatements for processing. The initial value of exception is the value of the error that occurred in tryStatements. If no error occurs, catchStatements are never executed.

If the error cannot be handled in the catchStatements associated with the tryStatements where the error occurred, use the throw statement to propagate, or re-throw, the error to a higher-level error handler.

After all statements in tryStatements have been executed and any error handling has occurred in catchStatements, the statements in finallyStatements are unconditionally executed.

Notice that the code inside finallyStatements is executed even if a return statement occurs inside the try or catch blocks, or if an error is thrown from a catch block. All finallyStatements are guaranteed to run unless an unhandled error occurs (for example, causing a run-time error inside the catch block).

Note

To avoid a potentially confusing situation, do not use a return statement in a finally block. The code in a finally block is run after a return statement in a try or catch block is encountered, but before that return statement is executed. In this situation, a return statement in the finally block is executed before the initial return statement, resulting in a different return value.

Examples

Description

The following example causes an error to be thrown and displays the error's message, code, and name.

Code

try
{
    var arr = new Array(-1);
}
catch(e)
{
    print ("Error Message: " + e.message);
    print ("Error Code: " + (e.number & 0xFFFF))
    print ("Error Name: " + e.name);
}

// Output:
//  Error Message: Array length must be zero or a positive integer
//  Error Code: 5029
//  Error Name: RangeError

Description

The following example shows how JScript exception handling works.

Code

try
    {
    print("Outer try is running");
 
    try
        {
        print("Nested try is running");
        throw new Error(301, "an error");
        }
    catch(e)
        {
        print("Nested catch caught " + e.message);
        throw e;
        }
    finally
        {
        print("Nested finally is running");
        }   
    }
catch(e)
    {
    print("Outer catch caught " + e.message);
    }
finally
    {
    print("Outer finally is running");
    }

// Output:
//  Outer try is running
//  Nested try is running
//  Nested catch caught an error
//  Nested finally is running
//  Outer catch caught an error
//  Outer finally is running

Requirements

Version 5

See Also

Reference

throw Statement

Error Object