VBScript Error Handling: VBScript On Error, On Error GoTo 0, On Error Resume Next
Posted by Superadmin on May 02 2019 03:09:43

VBScript Error Handling: VBScript On Error, On Error GoTo 0, On Error Resume Next

 

 

VBScript Error Handling: VBScript On Error, On Error GoTo 0, On Error Resume Next

Last Updated:April 23, 2019

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 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 the errors and to continue the execution of the program even after the occurrence of errors inside a program.

 

Error Handling Mechanism in VBScript

 

What You Will Learn: [show]

Overview

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 about 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 errors 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.

Which means, 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)
Dim result
result = 20/0 (Performing division by 0 Scenario)
If result = 0 Then (Checking value of result variable)
Msgbox “Result is 0.”
Else
Msgbox “Result is non-zero.”
End If

#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:

Let’s use the same Example in this case also:

Dim result
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)
End If

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.

Conclusion

I hope this tutorial must have provided an 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.