Thursday, May 29, 2008

On Error Statement

On Error statement enables or disables error handling.

Two On Error statements which we will discuss here are:

On Error Resume Next
On Error GoTo 0

If there is an error in the code "On Error Resume Next" ignores it and continues with the next line of the code.

Consider the below example. Write the below code in a notepad and save it as a .vbs file and run it from the command prompt. It works fine, but now if you comment or remove the "On Error Resume Next" line, it will show error because of the 7th line.

msgbox "Ha Ha 1"
call one
msgbox "Ha Ha 2"
Function one()

On Error Resume Next
msgbox "Function one Start"
two
msgbox "Function one End"
End Function
msgbox "Ha Ha 3"

Moreover "On Error Resume Next" is local to a function / procedure in which it is written. If it is written within a Function / Procedure then it will be in effect as long as the Function / Procedure executes and will be nullified (will not be in effect) when the Function / Procedure finishes execution.

msgbox "Ha Ha 1"
call one
msgbox "Ha Ha 2"
call two

Function one()

On Error Resume Next
msgbox "Function one Start"
happy
msgbox "Function one End"
End Function

Function two()

On Error Resume Next
msgbox "Function two Start"
happy
msgbox "Function two End"
End Function

msgbox "Ha Ha 3"

Run the above program, it will show the last message box "Ha Ha 3", but if you comment the "On Error Resume Next" of Function two() then you will not see the last message box "Ha Ha 3" because when the program finds the word "happy" in Function two() and no error handler, it raises a Type Mismatch error.

Note that with "On Error Resume Next" error is not corrected, just ignored, and an error message is not displayed.

Use On Error GoTo 0 to disable error handling if you have previously enabled it using On Error Resume Next.


Err Object

Whenever there is a run-time error in the program, the properties of an Err object are filled with the information that helps to identify and handle the error.

After an On Error Resume Next statement the Err object's properties are reset to zero or zero-length strings ("").

Because the Err object is an intrinsic (basic / its part of every vbscript project you create) object with global scope — there is no need to create an instance of it in your code. That is it does not need to be declared before it can be used.


PropertiesPurpose
DescriptionContains a string describing the error.
NumberContains the Error number. It is the default Property, means Err.Number is same as Err
HelpFileContains path to the help file
HelpContextIts a Context ID within the helpfile. The HelpContext property is used to automatically display the Help topic identified
Source Contains a string expression that is usually the class name or programmatic ID of the object that caused the error.
LastDLLErrorContains last error code generated by DLL;Available only on 32 bit windows systems.
MethodsPurpose
ClearClears all property settings of the Err object. [VBScript calls the Clear method automatically whenever any of the following statements is executed:
On Error Resume Next
Exit Sub
Exit Function ]
RaiseForces a run-time error of a given number to be generated.


msgbox "Ha Ha 1"
call one
msgbox "Ha Ha 2"
Function one()

msgbox "Function one Start"
happy
msgbox "Function one End"
End Function

Firstly, try to run the above code, when the program reaches the line which has 'happy', it will show Type Mismatch error.

Now run the below code which now has "On Error Resume Next" and Err object. This is just one of the million ways you can use Err object. You can also use Err number in conditional statements and loops etc to make the code more robust.

msgbox "Ha Ha 1"
call one
msgbox "Ha Ha 2"

Function one()

On Error Resume Next
msgbox "Function one Start"
happy
msgbox "Error number is = " & err.number & " and " & "Error Description = " & err.description
msgbox "Function one End"
End Function