How To Trap JScript Errors on a Web Page

Article translations Article translations
Article ID: 183616
This article was previously published under Q183616
Expand all | Collapse all


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.

More information

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:
<SCRIPT LANGUAGE="JScript" FOR="window" EVENT="onerror(msg,url,lineno)">
alert("window.onerror\n\n" +
"Error: " + msg + "\n" +
"URL:  " + url + "\n" +
"Line:  " + lineno);

return true; // Tell the browser not to report the error itself.   //-->

<SCRIPT LANGUAGE="JScript" FOR="button1" EVENT="onclick()">

<INPUT TYPE=button VALUE="Click for error" NAME=button1>
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


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
kbhowto KB183616
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from