VBScript Error Handling – On Error Resume Next, On Error Goto 0

VBScript Error handling in UFT automation scripts is very important like in any other language. We have some very common VBScript examples to demonstrate it. To show the use of different error handling statements in VBScript, we will use a function that divides an integer by zero (code given below) and produces a “Division by zero error”. Then we will use each error-handling statement using VBScript.

'Call  the function to divide by zero
'produces Division by zero error
Call division

Function division()
  
  'divide by zero
  z=40/0 ' This line gives Division by zero error
  a=10
  msgbox a
End function
division by zero
Division by zero
How to use – “On Error Resume Next” – VBScript Error Handling

On Error Resume Next statement enables the Error handling in the code. If there is an error in the code On Error Resume Next ignores it and continues execution with the next line of code. In the below example code, division by zero statements produces Division by zero error but it is ignored and the next line is executed.

'Call  the function to divide by zero
'produces Division by zero error
Call division

Function division()
  on error resume next
  'divide by zero
  z=40/0 ' This line gives Division by zero error
  a=10
  msgbox a

End function
vbscript error handling
On error resume next output
How to use On Error Goto 0 – VBScript Error Handling

On Error goto 0 statement disables error handling which we had enabled in our script by using On Error Resume Next.  In the below example, please see how the division by zero statement throws the Division by zero error again after disabling the error handling by using On error goto 0

'Call  the function to divide by zero
'produces Division by zero error
Call division

Function division()
  on error resume next
  'divide by zero
  z=40/0 ' This line gives Division by zero error
  a=10
  msgbox a ' This line executed by ignoring the error
  
  on error goto 0 ' This line disables the error handling
  x= a/0  'This line gives Division by zero error
  msgbox x  'This line is not executed

End function
vbscript error handling
On error resume next output
division by zero
err.number and err.description

Provides the error number and the description of the error

VBScript Error handling example
'Call  the function to divide
call division

Function division()
on error resume next

'divide by zero
z=40/0

' Report the  error occured. You can see the error number and description in result summary
If Err.number <> 0 then 
 Reporter.ReportEvent micWarning,"Error Occured","Error number is  " &  err.number & " and description is : " &  err.description

 'disables error handling
   on error goto 0

End if

End function

You may also read


Discover more from Automation Script

Subscribe to get the latest posts sent to your email.

Related Posts