Article ID: 183616
This article was previously published under Q183616
In Internet Explorer 4.01 and earlier, there is no language support for error handling in JScript. The only option in these versions is to handle the window.onerror event to trap errors. In Internet Explorer 5 and later, you can use the try...catch...finally statement to implement error handling in JScript.
How to Trap JScript Errors in Internet Explorer 4.01 and EarlierThe following code sample demonstrates how to use the onerror event of the window object to trap an error in JScript code:
NOTE: This code sample does not always work on a computer that has Microsoft Visual InterDev 6.0 installed, depending on which debugging options you select. If you turn off just-in-time debugging in Visual InterDev (from the Tools menu, click Options, and select Just-in-Time debugger), you cannot click the button on the page. If you turn on just-in-time debugging, you receive a run-time error message, and the onerror event is never fired. For this script to work with Visual InterDev 6.0, follow these steps:
How to Trap JScript Errors in Internet Explorer 5 and LaterThe try...catch...finally statement provides a way to handle errors that may occur in a given block of code while the code is still running. The try statements contain code where an error can occur, and catch statements contain the code to handle any error that may occur. If an error occurs in the try statements, program control is passed to catch statements for processing.
If the error cannot be handled in the catch statements that are associated with the try statements where the error occurred, use the throw statement to propagate, or rethrow, the error to a higher-level error handler.
After all statements in try statements have been executed and after any error-handling has occurred in catch statements, the statements in finally statements are unconditionally executed unless an unhandled error occurs (for example, if a run-time error occurs inside the catch block).
The following sample code illustrates the try...catch...finally statement:
This code generates the following output:
Outer try running.. Nested try running... Nested catch caught an error Nested finally is running... Outer catch caught an error re-thrown Outer finally running
For information on using JScript, see the following Microsoft Web site:
Windows Script Technologies
Article ID: 183616 - Last Review: July 19, 2012 - Revision: 4.0
Contact us for more help
Connect with Answer Desk for expert help.