Microsoft Office 2000/Visual Basic Programmer's Guide   

Handling Script Errors

You have limited run-time error-handling options with script behind an HTML page, and the conditions that generate errors can differ depending on the scripting language you use. For example, in VBScript, dividing by zero results in a run-time error. In JScript it does not.

In VBScript you are limited to basic in-line error handling using the On Error Resume Next statement. You can suppress default error messages but you can't specify your own error-handling code to run when an error occurs. After you have trapped and handled an error, you use the Clear method of the Err object to clear out the error information.

In JScript you can handle errors by using the window object's onerror event handler. The onerror event passes three arguments to the specified error-handling function: the error message itself, the URL representing the source of the error, and a number representing the line where the error occurred.

There are also special circumstances you should keep in mind when writing script and script-error handlers.

Script in an HTML page is not interpreted until the script is executed, which occurs at the earliest when an HTML page is loaded. This means that the language interpreter will not catch any syntax errors until the page is actually loaded.

Subtle logic errors can result from the required use of Variant variables with different data subtypes. For example, the statement "10" = 10 evaluates to True.

Script in the <HEAD> portion of the page is interpreted before script in the <BODY> portion of the page. A run-time error can occur if script in the <HEAD> portion of a page references elements within the <BODY> portion of a page and the script executes before the page is completely loaded.

Web pages consist of a number of different components that may be downloaded at different times. Because of this, you can't always be sure that an element of your page referenced in script is going to be available when the script executes. For example, if you use framesets, objects, images, linked style sheets, applets, or plug-ins, you must verify that the component is available before performing operations that depend on the component.