My Blog List

Wednesday, May 2, 2012

Error Handling in QTP

As we have 3 types of error handling methods in pure VB or VBA language, we can easily handle our errors like Try/Catch functionality. They are,
On Error Resume Next
On Error GoTo 0
On Error GoTo <LABEL>
But VBScript supports only 2. They are

On Error Resume Next
On Error GoTo 0
So here we are going to talk about these  2 handlers.


1. On Error Resume Next
This enables the vb error handler and whenever dynamic run time error occurs, it will collect the error information and continue with the next immediate statement. This won't stop the script execution when error occurs.
  1. On Error Resume Next
  2. Dim a
  3. a = 5 / 0   'this will give run time error - Divide by zero error
  4. msgbox "Script not stopped."
  5. msgbox Err.Description
So here 2 msgbox lines got executed. At the same time we are able to get the error information like I did in line 5. So you can check the error numbers and you can control your scripts in better way like,
  1. On Error Resume Next
  2. Dim a
  3. a = 5 / 0   'this will give run time error - Divide by zero error
  4. If Err Then
  5.     msgbox "Error occurs: " + Err.Number + "; " + Err.Message + "; " + Err.Description
  6. End If
Here is the list of Error codes
http://www.csidata.com/custserv/onlinehelp/vbsdocs/vbs241.htm
Also we will get negative error numbers also.


2. On Error GoTo 0
This is the default handler in VB. This will disables the handler and collects the error information and throw the error in msgbox. Mostly no one will use this.


Then, You can raise your custom error like,
  1. On Error Resume Next
  2. Err.Clear  'Use this to clear previously stored error info
  3. Dim a
  4. a = 5 / 0   'this will give run time error - Divide by zero error
  5. If Err.Number <> 0 Then
  6.   'Err.Raise code, message, description
  7.  Err.Raise 100"Please change the value""Division by Zero error occurs"
  8. End If
Don't forget to put Err.Clear as your error object may contain previously stored error info.

1 comment:

  1. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium

    ReplyDelete