Microsoft Office 2000/Visual Basic Programmer's Guide   

Handling JScript Run-Time Errors

In JScript, you can use the window object's onerror event to trap an error and respond accordingly. The onerror event is triggered when an error occurs anywhere on an HTML page. The onerror event passes three arguments to the function specified by the window.onerror = functionname; line. The arguments are the error message itself, the URL representing the source of the error, and a number representing the line where the error occurred. For example:

<SCRIPT LANGUAGE = "JavaScript">
   function BadFunction(){
      This.badcode.willnot.work = 1000
   }

   function ForceError(msg, url, lno) {
      alert("Error Occurred! Handled by Generic Error Handler" + "\n" +
         "Error: " + msg + "\n" + "URL: " + url + "\n" +   
         "Line Number: " + lno);

      return true;
   }

   window.onerror = ForceError;
</SCRIPT>

Note how returning True in the error-handler procedure forces the default message box not to appear. To see the entire sample, see JScriptErrors.htm in the ODETools\V9\Samples\OPG\Samples\CH08 subfolder on the Office 2000 Developer CD-ROM.

For more information about scripting HTML and ASP pages by using VBScript or JScript, see the Microsoft Scripting Technologies Web site at http://msdn.microsoft.com/scripting/.