VBScript – Error Handling

Introduction to Error Handling

‘Best Practice’ dictates that we should take steps to control errors.  Another reason for error handling is that it saves time troubleshooting when you can narrow down the problem to one section of the script.  In practice you have three, choices, write a statement that says ignore errors and carry on processing, allow errors to surface naturally, or add error handling statements (best), which will give you valuable information on where in the script the error is arising.

On Error Resume Next

What the ‘On Error Resume Next’ statement does is defer error handling, and suppress a system message.  The script will carry on running but you may be storing up problems.  There is counter statement called ‘On Error GoTo 0’  which turns off ‘On Error Resume Next’.  This means that you can have different error handling for different sections of you script.

 

On Error Resume Next
Err.Clear
Set objContainer = GetObject("LDAP://" & strOUContainer)

Note 1:  Without an ‘On Error Resume Next’ command, any run-time error will result in pop-up message box with an error message and the code will execute no further than the error.

Clear Method – err.Clear

Use the Clear method to explicitly reset the Err object once an error has been handled.  When you use deferred error handling with On Error Resume Next, you reset the err.raise to zero with a statement err.Clear.

Note 1: VBScript calls the Clear method whenever you Exit a Sub or Exit a function.

Example of error handling

 

If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
Wscript.Echo "Edit the LDAP path to your OU " & strOUContainer
Wscript.Quit
End If

Note 1:  This error handling routine uses the If … Then … End If construction.

Note 2:  Here is a classic use of the Wscript.Echo method.  The & strOUContainer is a neat touch to let you know in the message box exactly what is wrong with LDAP path.

Note 3:  If you actually see this error it means you need to find the variable strOUContainer and change the value to your domain.

Note 4: These error handling statements occur all through the script, each with an appropriate message.

The fact that VBScript is an interpreted language rather than a compiled language means that you can expect more runtime errors.  To trap these bugs you need to build in error handling statements which pinpoint coding errors such spelling mistakes in variables.

VBScript Error Trapping Example
Kindly sent by Paul DeBrino

 

‘ Test VBScript Error Trapping
‘ Written by Paul DeBrino – May 2012
‘ Capture output: cscript Test-VBScript-Error-Trap.vbs > trap.txt

Option Explicit
Dim oSh, objExec, iRet, iErrNum, bolStdErrNumber, strStdErrDesc

Set oSh = CreateObject("WScript.Shell")

‘ ————————————
WScript.Echo "TESTING QUIT-ONLY using SHELL EXEC"
Set objExec = oSh.Exec("cscript //nologo Quit-Only.vbs")
iErrNum = Err.Number

‘ Wait for task to complete:
Do While objExec.Status = 0
WScript.Sleep 100
Loop
iRet = objExec.ExitCode

If (NOT objExec.StdErr.AtEndOfStream) Then
bolStdErrNumber = True
strStdErrDesc = objExec.StdErr.ReadAll
Else
bolStdErrNumber = False
strStdErrDesc = "nada"
End If

WScript.Echo "Results…"
WScript.Echo "Err.Number: " & iErrNum
WScript.Echo "ExitCode: " & iRet
WScript.Echo "StdErr detected: " & CStr(bolStdErrNumber)
WScript.Echo "StdErr description: " & strStdErrDesc

‘ ————————————
WScript.Echo " "
WScript.Echo "Sleeping 5 seconds…"
WScript.Sleep 5000
WScript.Echo " "

‘ ————————————
WScript.Echo "TESTING QUIT-ONLY using SHELL RUN"
iRet = osh.Run("cscript //nologo Quit-Only.vbs", 0,True) ‘ 2nd arg "0" HIDES the new window
iErrNum = Err.Number

WScript.Echo "Results…"
WScript.Echo "Err.Number:" & iErrNum
WScript.Echo "ReturnCode:" & iRet

‘ ————————————
WScript.Echo " "
WScript.Echo "Sleeping 5 seconds…"
WScript.Sleep 5000
WScript.Echo " "

‘ ————————————
WScript.Echo "TESTING ERR-RAISE using SHELL EXEC"
Set objExec = oSh.Exec("cscript //nologo Err-Raise.vbs")
iErrNum = Err.Number

‘ Wait for task to complete:
Do While objExec.Status = 0
WScript.Sleep 100
Loop
iRet = objExec.ExitCode

If (NOT objExec.StdErr.AtEndOfStream) Then
bolStdErrNumber = True
strStdErrDesc = objExec.StdErr.ReadAll
Else
bolStdErrNumber = False
strStdErrDesc = "nada"
End If

WScript.Echo "Results…"
WScript.Echo "Err.Number: " & iErrNum
WScript.Echo "ExitCode: " & iRet
WScript.Echo "StdErr detected: " & CStr(bolStdErrNumber)
WScript.Echo "StdErr description: " & strStdErrDesc

‘ ————————————
WScript.Echo " "
WScript.Echo "Sleeping 5 seconds…"
WScript.Sleep 5000
WScript.Echo " "

‘ ————————————
WScript.Echo "TESTING ERR-RAISE using SHELL RUN"
iRet = osh.Run("cscript //nologo Err-Raise.vbs", 0,True) ‘ 2nd arg "0" HIDES the new window
iErrNum = Err.Number

WScript.Echo "Results…"
WScript.Echo "Err.Number:" & iErrNum
WScript.Echo "ReturnCode:" & iRet

‘ ————————————

Guy Recommends: Permissions Analyzer – Free Active Directory ToolFree Permissions Analyzer for Active Directory

I like thePermissions Monitor because it enables me to see quickly WHO has permissions to do WHAT.  When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, takes into account network share access, then displays the results in a nifty desktop dashboard!

Think of all the frustration that this free utility saves when you are troubleshooting authorization problems for users access to a resource.  Give this permissions monitor a try – it’s free!

Download Permissions Analyser – Free Active Directory Tool


See Also