Error handling is basically anticipating an error at any point of the script then detecting that particular error and finally resolving or handling the error.
Talking about QTP, errors could be classified in three ways:
2. Logical errors
3. Run time errors
Syntax errors are basically the typos or a part of the code that does not confirm with the grammar of the scripting language (here VBScript). Syntax errors occur at the time of compilation of code and can be easily detected before the execution of the code. To check syntax in QTP we can use the keyboard shortcut as Ctrl+F7.
A logical error is something that makes a part of code to produce unexpected results. Logical error does not halt or stop the execution but makes it to produce incorrect results. Logical errors could occur because of some misconception or wrong understanding of the requirement and the only way to detect a logical error is to correctly interpret the incorrect outcomes of that particular code snippet.
Run time errors are basically those errors which occur during the execution of the code (at run time). The basic reason of occurrence of these errors is when your script tries to perform a certain action but due to any reason that action is not possible to perform. Its like division by zero, file not found at the specified path, FOR loop not initialized etc .
There are multiple approaches to handle the errors in the QTP as in :
1. Synchronization Points.
2. Exist Property.
3. Recovery Scenarios.
4. On Error statement.
Here we would be discussing the fourth one, how to use On Error statement.
On Error statement enables or disables the error handling mechanism in the QTP. Enabling and disabling is done with the below mentioned two statements:
On Error Resume Next
On Error GoTo 0
If we have not used On Error Resume Next statement in our function then any run time error will appear as a pop up dialog and the script execution will stop at that particular code line.
On Error Resume Next statement makes your script to continue the execution in spite of that run time error. This statement will ignore that run time error and will move to the immediate next line of the code skipping the execution of that particular line which caused run time error.
Below mentioned simple example will illustrates the same.
a = 10
b = 0
c = a/b
msgbox c
Now, executing the above mentioned code will give a run time error for division by zero as shown below and script will stop, asking for the manual intervention.
But if we apply On Error Resume Next statement at the top, though script is logically incorrect but it will execute smoothly without any pop up dialog and will display a message box with no value as shown below:
On Error Resume Next
a = 10
b = 0
c = a/b
msgbox c
On Error GoTo 0 disables the error handling mechanism. The code part coming after the statement On Error GoTo 0 will run as it would have run without On Error Resume Next.
On Error resume Next
a = 10
b = 0
c = a/b
On Error GoTo 0
d = c/b
msgbox c & d
As illustrated in the above code, we will not be having any run time error till the line c = a/b but yes, QTP will give a run time error stating division by zero for the line d = c/b.
One more thing worth mentioning here is the Err object. Err object is already defined in the QTP with a global scope. Err object contains information about the run time generated error. The properties of the Err object are set by the run time error generator.
Whenever a run time error occurs, the properties of this Err object get automatically filled with details specific to that particular run time error. All properties of the Err object are reset to zero or null after the execution of the statement On Error Resume Next.
Few useful Methods associated with Err object are
Clear : Clears all property values to zero or null.
Raise : Generates a run time error.
Useful Properties assocaiated with Err object :
Number : Returns or sets a numeric value specific to a particular run time error.
Description : Returns the description associates with a run time error.
A possible way to handle error using Err object could be :
On Error Resume Next
a = 5
b = 0
c = a/b
If Err.Number = 0 Then
‘ Report Pass
Else
‘ Report Fail
End If
------------------------------------------------------------------------
Keep handling errors (:
--
# Hims
nice post
ReplyDeleteThanks Binit
DeleteNo new posts ??? .... waiting for some new insightful concepts...
ReplyDeleteRegarding for Query ?
ReplyDeleteWhen to use a Recovery Scenario and When to use “on error resume next”?
key features of QTP at a glance
http://www.bestqtptraining.com/