There are various ways to handle errors in the code.
1. Using Test Settings - Error handling can be defined the Test Settings by Navigating to "File" >> "Settings" >> "Run" Tab as shown below. We can select any of the specified settings and click "OK".
2. Using On Error Statement - On Error statement is used to notify the VBScript engine of intentions to handle the run-time errors by tester, rather than allowing the VBScript engine to display error messages that are not user friendly.
On Error Resume Next - On Error Resume Next informs the VBScript engine to process executing the next line of code when an error is encountered.
On error Goto 0 - This helps the testers to turn off the error handling.
3. Using Err Object - Error object is an inbuilt object within VBScript that captures the run time error number and error description with which we will be able to debug the code easily.
Err.Number - The Number property returns or Sets a numeric value specifying an error. If Err.Number value is 0 then No error had occured.
Err.Description - The Description property returns or sets a brief description about an error.
Err.Clear - The Clear method resets the Err object and clears all the previous values associated with it.
'Call the function to Add two Numbers Call Addition(num1,num2)
On error resume next If NOT IsNumeric(a) or IsNumeric(b) Then Print "Error number is " & err.number & " and description is : " & err.description Err.Clear Exit Function End If
Addition = a+b
'disables error handling On Error Goto 0
Using Exit Statement - Exit Statements can be used along with Err object to exit from a test or action or iteration based on the Err.Number value. Let us see each one of those Exit statements in detail.
ExitTest - Exits from the entire QTP test no matter what the run-time iteration settings are.
ExitAction - Exits the current action.
ExitActionIteration - Exits the current iteration of the action.
ExitTestIteration - Exits the current iteration of the QTP test and proceeds to the next iteration.
5. Recovery Scenarios - Upon encountering an error, recovery scenarios are triggered based on certain conditions and it is dealt in detail in a seperate chapter.
6. Reporter Object - Reporter Object helps us to report an event to the run results. It helps us to identify if the concerned action/step is pass/fail.