Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Trap JScript Errors on a Web Page

View products that this article applies to.

This article was previously published under Q183616

↑ Back to the top


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.

↑ Back to the top

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

↑ Back to the top


For more information about the onerror event, see the following Microsoft Web site: For more information about the try...catch...finally and throw statements, see the following Microsoft Web site: For information on using JScript, see the following Microsoft Web site:

↑ Back to the top

Keywords: kbhowto, KB183616

↑ Back to the top

Article Info
Article ID : 183616
Revision : 6
Created on : 8/27/2007
Published on : 8/27/2007
Exists online : False
Views : 442