Guy’s Scripting Ezine No 15 – Error Handling Techniques
Contents Error Handling Techniques
If there is one thing that I have learnt about computing it is this; users rarely do what you expect. Dare I take this one step further and say that budding scripters do not often apply my scripts as I anticipate? The greatest mismatch is when I design VBScripts for Windows 2003 with XP clients, only for them to fail on Windows 98 clients in NT 4.0 domains. Your challenge is to guess what the users will do, meanwhile, my challenge is to anticipate problems with non XP clients.
Guy Recommends: The Free IP Address Tracker (IPAT)
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.
For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker
Two scripting techniques
‘On Error Resume Next’ is like sticking your head in the sand. This is the equivalent of ignoring the problem and hoping that the error is not serious and will go away. ‘Option Explicit’ on the other hand, is like turning over a log and seeing what crawls out of the woodwork. Option Explicit forces you to check explicitly that everything is as it should be and there are no hidden flaws.
On Error Resume Next has two useful functions for logon scripts; firstly it suppresses error messages which could needlessly alarm users. Secondly, it means the you can salvaging at least some of the valid commands on later lines in the script.
If you are going to use On Error Resume Next, then do place it on the first line of your script. Where this on Error command comes into its own is if a server is down or a share is not available. In the example below, the idea is that even if the first command to map a network drive fails, you want the script will carry trying to map other drives.
‘ VBScript to demonstrate On Error Resume Next
on Error Resume Next
objNetwork.MapNetworkDrive “R:” , “\\banana\reports”
Note 1: Once the script works without error, try removing this line on
Solutions to real life Error Codes generated by this type of logon script
80070043 The network name cannot be found (Mistyped Server, or Share name)
For more errors codes – check here
Where you need to check for logic errors and to make sure that you have declared all you variables, insert Option Explicit at the start of your script.
To see the effect I have a non Windows VBScript example. The purpose of my VBScript converts Celsius (Centigrade) into Fahrenheit. The learning point is to see how a logical error creeps in if I do not use Option Explicit. What happens is the script executes, you get an answer but the result is incorrect.
‘ VBScript to demonstrate Option Explicit
intResponse = inputbox(“Please type Temperature in Celsius.”, “Guy’s Temperature Demo”)
if isnumeric (intResponse) then
intDegreesFahrenheit = ConvertToFahrenheit(intDegreesCelsius)
Wscript.Echo intDegreesCelsius & ” Centigrade converts to: ” & intDegreesFahrenheit _
Function ConvertToFahrenheit(ByVal intDegreesCelsius)
‘ Deliberate Guy Error in Next line intDegreesCelsius
Note 1: Extra the error handling code (below) to cater for people entering letters instead of numbers.
if isnumeric (intResponse) then
So you have decided to correct errors. There are three places to look for problems with your scripts, syntax, runtime and logical errors.
Breaches of syntax rules are easy to spot because the error message contains the offending bracket or punctuation mark. e.g. Expected ‘)’ or Expected ‘If’. Moreover, syntax errors begin with 800Axxxx. There are 53 types of syntax errors and here are some of the common reasons for syntax violations.
Check formula, add brackets where necessary. Remember that & (ampersand) joins text where as + adds numbers. Divide by zero is less likely in our types of scripts.
For more about code errors see here.
Classic case of investing a little time upfront, which then pays back later with interest. Even the simplest scripts benefit from error handling and debugging.
Use a naming scheme, preferably with Hungarian Variables e.g. begin with int, or str
Declare and initialize all variables. Use Option Explicit.
Break a long script into sections, reuse chunks of code that you have already tested.
Look out for endless loops.
Perhaps the biggest mistake is to go for an ‘all bells and whistles’ script instead of getting a basic model working and only then adding extra components.
Go for that professional finish, declare variables, anticipate problems, add error handling code. Time spent on error handling will improve your understanding of what the script can and cannot achieve.
For more details on error codes – Error Codes
See more about VBScript error correcting codes