VBScript Error Handling

What Error handling routines do your scripts use?

Best Practice (Litmus Test)

Professionals:  Use Error handling code

Amateurs:  Trust to luck


Error Handling in VBScripts

One of the last jobs in writing scripts is adding the error code.  Professionals go that extra mile because they know that prevention is better than cure.  Time spent in defensive code writing will pay off handsomely when it comes to troubleshooting.

Example of Error Handling VBScript

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

Note 1:  The Wscript.Echo method creates a pop-up message telling you where the script execution stopped.  The & strSpreadPath is a neat touch to let you know exactly what is wrong with path to the Excel file.

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

Because VBScript is not a compiled script 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.

On Error Resume Next

Another tactics for VBScript error handling include ‘On Error Resume Next’.  This can be useful for skipping errors in logon scripts, especially where you have slow links and a server with shares could be unavailable.

Option Explicit

Option Explicit may actually induce more errors in your script.  However, the advantage of adding this command is that it will highlight undeclared variables.  "Best Practice’ dictates that all variables should be declared in a DIM statement at the start of the script.  See more on VBScript error handling.

See more about VBScript here.

PowerShell Error Correcting -ErrorAction Stop

This is how I found that -ErrorAction has an alternative to SilentlyContinue:
-ErrorAction Stop

# Research PowerShell -ErrorAction
Clear-Host
Get-Help about_commonParameters

Note 3:  Incidentally, Help about_common* works just as well.

Note 4:  Other scripts may benefit from substituting Stop or Inquire for the action to SilentlyContinue.

-ErrorAction Abbreviations or Aliases

Instead of ErrorAction SilentlyContinue you can try : -EA 0

# PowerShell Registry Listing
Clear-Host
Set-Location HKLM:\
Get-Childitem -EA 0

Researching the about_commonParameters file help file will explain why these also work:
-EA 1 Continue
-EA 2 Inquire
-EA 3 Confirm
-EA 4 Stop

See more on PowerShell and Error Correcting »

Download my Jumbo Litmus Test eBook $5.95

Litmus TestsOver 40 of Guy’s litmus tests.  Have fun while you learn about aspects of computing.  Stacks of ideas to check your servers, networks and security.

Your eBook has printer friendly pages and lots more screen shots.

 

Litmus Tests

Guy’s Litmus test is a concept that you can apply anywhere.  Each test gives you an instant answer to the simple question:-  ‘Are you dealing with a professional, or are they an amateur?   Is this the real deal, or is it a turkey?’   The Litmus Test concept is rather like Best Practice, but it reduces a 27 page report to one sentence. 

Try another of my Litmus tests »


Learn about Windows 8 and Active Directory

Windows 8 New Features   • Windows 8 Remote Desktop   •Windows 8 Install and Boot

Windows 8 Performance  •Windows 8 Overview  •Permissions

Organization Units Litmus Test  • Universal Groups  Permissions Analyzer