On Error

Error handling in VBScript is very basic, there is no option to immediately branch to an error handling routine.
VBScript does not support GoTo or labels that would allow a program jump.

When error handling is enabled, execution will continue onto the next line. To trap an error you have to explicitly test the err object to see if it holds an error.

Syntax 
      On Error resume next    - Enable error handling

      On Error goto 0         - Disable error handling

Error properties:

   err.Number (default)
   err.Source
   err.Description

Examples

In the examples below - replace the 'code goes here' line with your VBScript commands.

Example 1) Trap an error

On Error Resume Next

' code goes here
If Err.Number <> 0 Then
'error handling: WScript.Echo Err.Number & " Srce: " & Err.Source & " Desc: " & Err.Description
Err.Clear
End If

Example 2) Trap an error or success

On Error Resume Next
' code goes here
If Err.Number = 0 Then WScript.Echo "It worked!" Else
WScript.Echo "Error:" WScript.Echo Err.Number & " Srce: " & Err.Source & " Desc: " & Err.Description
Err.Clear
End If

Example 3) Trap an error

On Error Resume Next

' code goes here
If Err.Number <> 0 Then ShowError("It failed")
Sub ShowError(strMessage) WScript.Echo strMessage WScript.Echo Err.Number & " Srce: " & Err.Source & " Desc: " & Err.Description
Err.Clear End Sub

“Success is falling nine times and getting up ten” ~ Jon Bon Jovi

Related:

Syntax - error codes
InputBox - Prompt for user input
Equivalent in PowerShell: ErrorAction and $errorActionPreference


 
Copyright © SS64.com 1999-2019
Some rights reserved