What is the use of 'On Error Resume Next' in QTP?

On many occasions, the execution has stopped if any error occurs during execution. But in some cases, we wanted to continue the execution even if any error occurs. QTP supports certain methods which allow scripts to run even if certain errors occurred.

On Error Resume Next

We need to include 'On Error Resume Next' at the beginning of the step in which we are anticipating some errors. This command will handle the error and 

continue the execution of the script.

Considering the below script, I know the value of 'a' but the value of 'b' is not constant and it may change based on other steps in the script.

a = 10
b = <Dynamic value>
Msgbox a/b

If the 'b' value is '0' then 'Division by Zero' error will be displayed. In this situation, we can add error handling command to handle this.

On Error Resume Next
a = 10
b = 0
Msgbox a/b
If Err.Number <> 0 Then
 Msgbox Err.Description
End If

With this example, the error will be handled and the actual error message will be displayed in the message box. The script will continue to execute without showing error/exit.

Now, we have activated this feature and If we don't want to have this enabled further, we can deactivate this error handling feature by below command.

On Error Goto 0

Putting all to gether,

On Error Resume Next
a = 10
b = 0
Msgbox a/b

If Err.Number <> 0 Then

 Msgbox Err.Description
End If

On Error Goto 0

Msgbox a/b

While executing this script, when first time error occurs it will be displayed in the Message box. Because at the first step we enabled the error handling feature. At the 10'th step we disabled this feature. So error will not be handled for all the steps that executed after Step 10.



To receive daily posted JOBS & Interview Questions
Just enter your email address below and click 'Submit'
Enter your email address:

Make sure to activate your subscription by clicking on the activation link sent to your email