Tuesday, October 9, 2012

On Error Resume Next

   Hello All! Happy to be in touch with you all through this blog. In this post, we are going to see a way of error handling. On many occasions, the execution has stopped if any error occurs during exection. But in some case, 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 script or before 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.


No comments:

Post a Comment