• No results found

Error Handling Flag and the "Err" Object

In document VBScript Handout (Page 72-76)

Learning Objectives

This Session provides brief information on runtime error handling. Topics include the default error handling behaviour; 'On Error Resume Next' and 'On Error GoTo 0' statements; catching errors with (Err.Number > 0); clear Err object with Err.Clear(); raise your own errors with Err.Raise(...).

 Error Handling Rules Overview

 IE Option Setting - Enable Script Debugging

 "On Error Resume Next" - Turning on Error Handling  "On Error GoTo 0" - Turning off Error Handling

 "Err.Number" and "Err.Clear()" - Error Code and Clear Method  Built-in "Err" Object Properties and Methods

 "Err.Raise()" - Raising Your Own Errors

Error Handling Rules Overview

VBScript offers some limited facilities to help your manage runtime errors. Here is a summary of rules that will help you to properly manage runtime errors in your VBScript code:

1. The "On Error Resume Next" statement can be used to turn on the error handling flag. 2. The "On Error GoTo 0" statement can be used to turn off the error handling flag.

3. User defined procedures have their own error handling flags. This means that each user defined procedure should be having its own "On Error Resume Next" or "On Error GoTo 0" statement to turn on or off its own error handling flag.

4. By default, error handling flags of all user defined procedures and the main code are turned off. 5. By default, the host environment provides a built-in object called "Err" to hold runtime error information.

6. When a runtime error occurs, the host environment will set values to properties of the "Err" object and raise a runtime error.

7. When a runtime error is raised and the current error handling flag is on, the host environment will continue to execute the next statement.

8. With the error handling flag turned on, you can inspect "Err" object properties to determine how to handle the error and continue your code execution.

9. When a runtime error is raised in a procedure and the current error handling flag is off, the host environment will stop executing the procedure, transfer execution control to the calling code, and raise the same runtime error in the calling code.

10. When a runtime error is raised in the main code and the current error handling flag is off, the host environment will stop executing the main code, and take some additional steps.

11. When Internet Explorer (IE) gets a runtime error with the error handling flag turned off, it will display small dialog box, if its script debug option is turned on.

12. When Windows Script Host (WSH) gets a runtime error with the error handling flag turned off, it will print out an error message.

IE Option Setting - Enable Script Debugging

Since we have been using Internet Explorer (IE) as the host environment in previous sections, let's look at what will IE do when the VBScript raises a runtime error.

IE 6.0 supports an option called "Disable Script Debugging (Internet Explorer)" on the "Advanced" tab. Let's uncheck it, meaning enable script debugging as shown in the picture below:

Now run the following VBScript example:

Example:

Dim x

X = 1/0

You should get a dialog box with this message: A Runtime Error has occurred.

Do you wish to Debug? Line: 8

Error: Division by zero

What happened in the example: What happened in the example:

When IE tries to execute the "x = 1/0" statement, a runtime error "Division by zero" is When IE tries to execute the "x = 1/0" statement, a runtime error "Division by zero" is raised.

raised.

IE stops executing the rest of the VBScript code, because the error handling flag is turned IE stops executing the rest of the VBScript code, because the error handling flag is turned off by default.

off by default.

IE displays a dialog box asking your confirmation to start the debugging tool. IE displays a dialog box asking your confirmation to start the debugging tool.

"Err" object properties are printed on the dialog box, telling us the type of error, and where "Err" object properties are printed on the dialog box, telling us the type of error, and where it occurs.

it occurs.

"On Error Resume Next" - Turning on Error Handling

"On Error Resume Next" - Turning on Error Handling

We have seen what happens when the error handling flag is turned off in the previous section. We have seen what happens when the error handling flag is turned off in the previous section. Now let's see how the "On Error

Now let's see how the "On Error Resume Next" statement should be used:Resume Next" statement should be used: By default, the error handling flag is turned off.

By default, the error handling flag is turned off.

You can turn on the error handling flag at time your want by entering the "On Error You can turn on the error handling flag at time your want by entering the "On Error Resume Next" statement.

Resume Next" statement.

Once the error handling flag is turned on, execution will not be stopped when a runtime Once the error handling flag is turned on, execution will not be stopped when a runtime error occurs.

error occurs.

You can use the condition of (Err.Number>0) to determine a runtime error has occurred or You can use the condition of (Err.Number>0) to determine a runtime error has occurred or not.

not.

If a runtime error has occurred, use Err object properties to get more information about the error: If a runtime error has occurred, use Err object properties to get more information about the error:

Err.Number - "Err" object property containing the error code. Err.Number - "Err" object property containing the error code.

Err.Description - "Err" object property containing the error description. Err.Description - "Err" object property containing the error description. Err.Source - "Err" object property containing error source identification. Err.Source - "Err" object property containing error source identification.

"On Error GoTo 0" -

"On Error GoTo 0" - Turning off Error HandlingTurning off Error Handling

If you want to catch the first runtime error is a large section of code, you need to: If you want to catch the first runtime error is a large section of code, you need to:

Enter the "On Error Resume Next" statement in the main code to turn on the error Enter the "On Error Resume Next" statement in the main code to turn on the error handling flag for the main code.

handling flag for the main code.

Put that section of code into a new subroutine procedure. Put that section of code into a new subroutine procedure.

Enter the "On Error Goto 0" statement in the new procedure to turn off the error handling Enter the "On Error Goto 0" statement in the new procedure to turn off the error handling flag for that procedure.

flag for that procedure.

Check the Err.Number property right after calling that procedure Check the Err.Number property right after calling that procedure

"Err.Number

Built-in "Err" Object Properties and Methods

Built-in "Err" Object Properties and Methods

1. "Err" is a built-in object representing the last runtime error that occurred during the execution. 1. "Err" is a built-in object representing the last runtime error that occurred during the execution. 2. "Err" has the following important properties.

2. "Err" has the following important properties.

Err.Number - The code value of the runtime error. For example, Err.Number = 11 is the Err.Number - The code value of the runtime error. For example, Err.Number = 11 is the code value of the "Division by zero" err

code value of the "Division by zero" error. See the list of or. See the list of common error code values below.common error code values below. Err.Description - The description string of the runtime error.

Err.Description - The description string of the runtime error. Err.Source - The source string of the runtime error.

Err.Source - The source string of the runtime error. 3. "Err" has the following important methods.

3. "Err" has the following important methods.

Err.Raise (number, source, description) - Raises a new runtime error with your own error Err.Raise (number, source, description) - Raises a new runtime error with your own error code, source and description.

code, source and description.

Err.Clear () - Clears the current runtime error. Err.Clear () - Clears the current runtime error.

"Err.Raise()" - Raising Your Own Errors

"Err.Raise()" - Raising Your Own Errors

VBScript also allows you to use the "Err" to r

VBScript also allows you to use the "Err" to raise your own runtime error aise your own runtime error with the Err.Raise()with the Err.Raise() method:

method:

Call Err.Raise(number, source, description) Call Err.Raise(number, source, description) Where:

Where:

"number" is an integer in the range of 0 and 65535 to represent a specific error condition. "number" is an integer in the range of 0 and 65535 to represent a specific error condition. But lower values are already used

But lower values are already used by VBScript for predefined errors. So by VBScript for predefined errors. So you should useyou should use higher values, like 60000, 60000, ...

higher values, like 60000, 60000, ...

"source" is a string to identify where the error occurred. "source" is a string to identify where the error occurred. "description" is a string to describe the error condition. "description" is a string to describe the error condition.

Summary

Summary

Runtime errors will be managed by t

Runtime errors will be managed by t he host environment, if you don't manage them.he host environment, if you don't manage them. "On Error Resume Next" turns on the error

"On Error Resume Next" turns on the error handling flag, which causes the hosthandling flag, which causes the host environmen

environment to t to continue execucontinue executing the next statement after raising a ting the next statement after raising a runtime error.runtime error. "On Error GoTo 0" turns

"On Error GoTo 0" turns off the error off the error handling flag, which causes the host environmenthandling flag, which causes the host environment to stop executing the current procedure after raising

to stop executing the current procedure after raising a runtime error.a runtime error. The "Err" object is built-in object

The "Err" object is built-in object representing the last runtime error.representing the last runtime error. "Err.Number > 0" can be used to see if a

"Err.Number > 0" can be used to see if a runtime error has occurred or not.runtime error has occurred or not. "Err.Clear()" method can be used to

"Err.Clear()" method can be used to clear the "Err" object.clear the "Err" object. "Err.Raise()" method can be used to raise

In document VBScript Handout (Page 72-76)

Related documents