Err object holds useful information about the error that occurs during script execution.
The Err objects provide us the following properties:
- Number– It provides the error information that occurred last.
- Source- It provides the last known component name from where the error occurred.
- Help File– It provides the path of the help file that is associated with Windows help file.
- Help Context– It provides a unique identifier if the error associated with a window help file.
The Err object also provides two methods:
- Clear()- This method clears or erases all properties of an Err object.
- Raise()-This method helps us to generate a custom runtime error.
On Error Statement
On Error is kind of an alternative to try catch. An upgraded error handling mechanism for VBScript or UFT among all other mechanisms.
On Error statement has two flavors
- On Error GoTo Next
- On Error GoTo 0
On error GoTo Next
As the name suggests, it suppresses all the errors and transfers the control to the next line. It just tells the VBScript engine to ignore the errors.It suppresses the errors from the whole script (from functions, subs, classes etc). It feels very good to see that the scripts we have written do not have any error but this is very risky in real time scenario. We have to be very sure about the error before suppressing the same.
On Error GoTo Next /On Error Resume Next can be localized just like a variable.This isolates the problem area from the remaining script.
On Error GoTo 0
While On Error Resume Next temporarily disables the error reporting, On Error GoTo 0 immediately enable error reporting. However, even though it is very useful but the automation team uses it very rarely.
Option Explicit Dim myVar On Error Resume Next myVar=getValueFromFlatFile() If(Err.Number=0) Then msgbox "No error" +myVar Else myDisplayError End If on Error GoTo 0 private function getValueFromFlatFile() Err.Raise VbObjectError+1000,"getValueFromFlatFile","An error has occured" End Function private Sub myDisplayError() msgbox "An error has occured" End Sub
This code On Error Resume Next and On Error GoTo 0 acts as try-catch block(used to handle error or exception in advanced languages).The code block expects an error from the getValueFromFlatFile() function and traps that.
Err.Number property is responsible for displaying relevant error information about the displaying error.
The guideline for setting up a trap or SafetyNet around the risky Code
- We can use On Error Resume Next just before the risky code.
- The risky code produces an ErrNumber as soon as the error occurs. If no error occurs it produces 0(zero).
- If an error occurs, we need to handle by any mean by using a loop or using email sending feature.
- Once the error is handled properly, we need to transfer the control back to its normal mode by On Error GoTo 0 statement.
- VBScript does not have a generic way of handling Error Exception(just like the Exception class in Java).
- We can set up multiple error trap to catch multiple errors.
- Generally, we use Wscript.Quit() method to stop the further execution of the script if we receive an error.
- In place of WScript.Quit() function we can also call any custom cleanup methods per our need. This is kind of finally block of java.
- In UFT/QTP we tend to put error handling code in respective functions, which logs and displays the sequential error message. This approach is often criticized as a poor or bad design principal.
What if We do not use Error trapping technique?
In this case, Wscript host(WHS) will take control and will display error when it occurs. This technique gets visible with a popup which will not be doing all cleanup operations.
The popup mostly not self-closing hence may stop the batch execution. This is also a major roadblock of unattended execution while moving towards CDCTCI to devops. Hence it is considered a very inefficient technique.
However during the debugging process, to understand the error better, I would suggest not to trap the error info. Instead, display it in a user-friendly manner using Error objects various properties like
The upgraded way to move forward and implement CDCI or devops is to log the error message to a file by implementing logger functionality in a cleanup function.
VBScript Custome Error
VBScript also provides an option to generate a custom error. This feature enables us to react to an error in a user-friendly manner. When we do scripting, we consider several opportunities which may fail the script. We call them assumptions. Using code we try to cover as many assumptions as possible. These assumptions become rules or requirements for us. If in a case, we can not convert directly by code, we create a custom error.I n general, all possible cases need to be coded.
The syntax for custom error:
The Err.Raise() method enables us to simulate errors when we test our error handlers.
Err.Raise() method takes five arguments
- Err.Number- The range of custom error number is between 0 to 65535. However, 0 is not used in many cases as sometimes developer treats o as not an error.
- Err.Source- This is an optional parameter indicating the origin of the error.
- Err.Description-This is also an optional parameter indicating additional information about the error. (It is treated as Metadata about the error.)
- Helpfile-If the error corresponds to a VBScript helpfile, this info is displayed. We need to provide the path of the help file.
- If the error corresponds to a VBScript help file, this info is displayed. We need to provide the path of the help file.
In a script if there are two consecutive errors come, the first error is overwritten by the second error.To get the correct error reporting it is good to clear the Err object.
If Err>0 then Err.Number=9999 Err.Description="A description that matches custom error" Mylogger.log(Err.Number & Err.Description) //this is just to log the Error Err.Clear End if
VBScript engine will automatically execute Clear() method when it encounters On Error Resume Next statement or Exit Sub or Exit Function statements .
When we should use Err.Raise() method?
Error information helps the coders to understand the actual issue. If in case Err.Raise() is used, it will automatically display the masked error message not the original error message. This will make the developers job tough. They will not be able to detect the original issue. Hence during development Err.Raise() should not be used but in production, it should be used.