Microsoft KB Archive/183616

= How To Trap JScript Errors on a Web Page =

Article ID: 183616

Article Last Modified on 8/27/2007

-

APPLIES TO


 * Microsoft JScript 1.0
 * Microsoft JScript 2.0
 * Microsoft JScript 3.0
 * Microsoft JScript 4.0
 * Microsoft JScript 5.0
 * Microsoft JScript 5.5
 * Microsoft Internet Explorer 3.0
 * Microsoft Internet Explorer 3.01
 * Microsoft Internet Explorer 3.02
 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q183616



SUMMARY
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 Earlier
The 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:
 * 1) In Internet Explorer, click Internet Options from the Tools menu.
 * 2) On the Advanced tab, select the Disable Script Debugging check box.

How to Trap JScript Errors in Internet Explorer 5 and Later
The 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: try { print("Outer try running.."); try { print("Nested try running..."); throw "an error"; } catch(e) { print("Nested catch caught " + e); throw e + " re-thrown"; } finally { print("Nested finally is running..."); }  } catch(e) { print("Outer catch caught " + e); } finally { print("Outer finally running"); } 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

