Error Handling in VBScript


ErrorHandlingError handling is the way for responding to occurrence of some unexpected situation that arrives during the computation. In automation testing, these unexpected situation can be like objects of application getting changed or some mathematical manipulation etc.

For handling such situation Visual Basic Script Edition (VBScript) provides us some of the methods. The details and usage of these methods are as follows:

On error resume next

This statement specifies that when any run time error occurs at particular line in script the control goes to the next line following the statement where the error as occurred.

For example: We are performing a division by zero, and if such situation occurs we dont want script to be interrupted. So we will be adding “On error resume next” statement at the top of the script.

On error resume next
If Div=0 then
Msgbox “Pass”
Msgbox “Fail”
End if

On error go to 0

Disables any enabled error handler and reset it to nothing

Err object

Err object is the intrinsic object with global scope means there is no need  to create the instance of it for accessing the various methods of  it.The details of all the methods of err object can be find in the below table:

Err Properties Details
Number Returns the integer value telling the type of   the error occurred
Description Gives the reason for the occurrence of the   error
Err Methos Details
Clear Helps to reset the error handler to nothing   once the error has been handled.

Lets take the example of division by zero again.

'Call  the division function
call division
Function division()
on error resume next
'divide by zero
' Report the  error occured. You can see the error number and description in msgbox
If Err.number <> 0 then
Msgbox “Error Number” + Err.Number
Msgbox “Error Description” + Err.Description
'disables error handling
on error goto 0
End if
End function

In Visual Basic, we have two more methods available for error handling

On Error Goto Line – Moves the control to a particular line number.

On Error Goto Label – Moves the control to  a particular section of code which is defined in a label.

These two methods are not available in VBScript.




Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.