Error handling. For scripts designed to run against multiple computers or printers it is important to including error handling in case the remote machine is off-line.
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