Introduction to VBScript Error Handling: Tutorial #14
In my previous tutorial, we discussed ‘File Objects’ in the VBScript. In this tutorial, I will brief you on the Error Handling mechanism that is used in the VBScript with methods like VBScript On Error, On Error GoTo 0, On Error Resume Next.
=> Also check all VBS Tutorials in this series here.
Error Handling is a very useful mechanism of programming languages like VBScript in order to deal with errors and to continue the execution of the program even after the occurrence of errors inside a program.
What You Will Learn:
Error Prevention is an aspect of Error handling which means taking effective measures inside a script in order to avoid the occurrence of errors.
Errors may include any of the following:
#1) Making use of Exist property to check the existence of an object before making any operations on the same.
#2) Synchronization techniques to deal with the delay and wait for operations inside a script.
#3) Making use of an Option Explicit statement to avoid misspelled words or spelling problems.
Now, let’s dive deep into Error Handling as it’s important to understand how to deal with the errors while working with the scripts.
Purpose of Error Handling
The main purpose of performing a testing activity is to find and resolve the errors. Though it is not possible to have a 100% error Free S/W, still you can take measures to bring down the error count as much as possible by making use of Error Handling Mechanism in your scripts.
Situations like issues in mathematical computations or any type of error can be handled with the help of Error Handling.
Now, Let’s see some of the methods of Error Handling in the VBScript.
Methods of Error Handling in the VBScript
VBScript basically supports 2 main methods to handle errors in the scripts.
They are as follows:
#1) On Error Resume Next
Most of us must have come across this method in some of the other programming languages. This method, as the name itself suggests, moves the control of the cursor to the next line of the error statement.
This means, that if any runtime error occurs at a particular line in the script then the control will move into the next line of the statement where the error has occurred.
A Simple Example:
In this case, the division is by 0 and if you do not want your script to get stuck due to this error then you put ‘On Error Resume Next’ at the top of your script as shown below.
On Error Resume Next (Putting error handling statement)
result = 20/0 (Performing division by 0 Scenario)
If result = 0 Then (Checking the value of result variable)
Msgbox “Result is 0.”
Msgbox “Result is non-zero.”
#2) Err Object:
This method is basically used to capture the details of the Error. If you want to know more about the Error like Number, description, etc., then you can do so by accessing the properties of this Object.
As this is an intrinsic object, there is no need to create an instance of this object to access its properties i.e. you can use this directly in your scripts.
Following is the list of properties of Err Object with their details:
Number: This will tell you the error number i.e. the integer value of the type of error that occurred.
Description: This will tell you about the error i.e. the description of the error.
Raise: This will let you raise the specific error by mentioning its number.
Clear: This will clear the error i.e. will set to error handler to nothing.
Let’s use the same Example in this case also:
on error resume next
result = 20/0 ‘(Performing division by 0 Scenario)
If Err.Number <> 0 Then ‘(Making use of Err Object’s Number property)
Msgbox “Number of the Error and Description is “& Err.Number & ” ” & Err.Description ‘(Give details about the Error)
Err.Clear ‘(Will Clear the Error)
on error goto 0
One more to the list:
#3) On Error GoTo 0:
This method is however not an Error Handler mechanism directly because this is used to disable any error handler that is used in the script. This will set the handler to nothing i.e. no more error handler will be supported in the script.
Also, read =>> How to handle errors in VBA
I hope this tutorial must have provided insight regarding the importance and effectiveness of using Error Handling. This tutorial will, in turn, help you in dealing with the VBscript errors in a more effective manner.
Finally, this is the last topic of the VBScript tutorial series.
Next Tutorial #15: I will cover some interview questions related to VBScript in my next tutorial which will include questions from all the topics that I covered till now in this series.
Share your experience on using Error Handling Mechanism and let us know if you have any queries.