VBScript Language Syntax

Handling Errors

Just as in traditional Visual Basic applications, an error handling script can be included on a Web page.

On Error Statement

You can change the default error handling behavior for a Web page by adding the On Error Resume Next statement to the beginning of your script. This statement instructs Internet Explorer to continue running the script. For each procedure in which you want error handling enabled, you must include the On Error Resume Next statement.

If an error does occur, Internet Explorer stores the error information in the Err object. If another error occurs, the Err object will be updated with the new error, and the old error will be overwritten.

To turn off error handling, use the statement On Error GoTo 0 . Internet Explorer will revert to the default error handling behavior and display the error message to the user .


Note

VBScript does not support the On Error GoTo < label > statement, and you cannot write an error handler that is called automatically when an error occurs. Therefore, you must implement inline error handling to check for an error after each statement that can cause an error.


Err Object

To detect run-time errors, check the Number property of the Err object after each statement that might cause an error. If Number is zero, an error has not occurred. If it is not zero, an error has occurred.

To retrieve information about the error, check the Description property.

When an error occurs, the Err object contains the error information until another error occurs. If a statement runs successfully, the Err object is not cleared. Therefore, after an error occurs, clear the error by invoking the Clear method of the Err object.

Example

This example shows the general syntax for handling errors:
 Sub cmdSubmit_OnClick On Error Resume Next 'Statement that might cause an error If Err <> 0 Then Msgbox "An error occurred. " & Err.Description Err.Clear End if 'Statement that might cause an error If Err <> 0 Then  Msgbox "An error occurred. " & Err.Description Err.Clear End if End Sub 
To test your own error-handling code, you can purposely cause an error by using the Raise method of the Err object.

VBScript does not use all available numbers for its errors. If you want to generate your own errors, begin a numbering scheme with 65535 (the highest value supported) and work your way down.

Example

This example would cause a custom error numbered 65000 to be raised:
 Err.Raise 65000 


Microsoft Windows Architecture Training
Microsoft Windows Architecture for Developers Training Kit
ISBN: B00007FY9D
EAN: N/A
Year: 1998
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net