Collapse AllExpand All        Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: JScript 
try...catch...finally Statement (Windows Scripting - JScript)

Updated: August 2009

Implements error handling for JScript.

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

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.

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 rethrow, 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 the catch block re-throws the error. finallyStatements are guaranteed to always run, unless an unhandled error occurs (for example, causing a run-time error inside the catch block).

Example

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

JScript
try
{
    // Cause an error.
    var x = y;
}
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);
}

The following example shows how JScript exception handling works.

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


function ShowLine(s)
    {
    document.write(s);
    document.write ("<br />");
    }

This produces the following output:

Outer try running..
Nested try running...
Nested catch caught an error
Nested finally is running...
Outer catch caught an error
Outer finally running
Requirements

Version 5

See Also

Reference

Change History

Date

History

Reason

August 2009

Modified second example.

Information enhancement.

April 2009

Added another example.

Customer feedback.

March 2009

Reformatted example.

Information enhancement.

Tags :


Community Content

Cornan The Iowan
"exception" is a JavaScript "error" object
The initial value of "exception" is a JavaScript Error Object (http://msdn.microsoft.com/en-us/library/dww52sbt(VS.85).aspx)
Tags :

Page view tracker