Guy’s Scripting Ezine No 15 – Error Handling TechniquesContents Error Handling Techniques
This Week’s SecretIf 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 NextOn 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. Example 1 On Error Resume NextInstructions
‘ 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 Option ExplicitWhere 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. Example 2 Option ExplicitInstructions
‘ VBScript to demonstrate Option Explicit intResponse = inputbox(“Please type Temperature in Celsius.”, “Guy’s Temperature Demo”) if isnumeric (intResponse) then else end if intDegreesFahrenheit = ConvertToFahrenheit(intDegreesCelsius) Wscript.Echo intDegreesCelsius & ” Centigrade converts to: ” & intDegreesFahrenheit _ Function ConvertToFahrenheit(ByVal intDegreesCelsius) ‘ Deliberate Guy Error in Next line intDegreesCelsius End Function Note 1: Extra the error handling code (below) to cater for people entering letters instead of numbers. if isnumeric (intResponse) then else Common errors and how to avoid themSo you have decided to correct errors. There are three places to look for problems with your scripts, syntax, runtime and logical errors. SyntaxBreaches 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.
Runtime
Logic ErrorsCheck 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. Defensive ProgrammingClassic 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. SummaryGo 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• VBScripts • 800 Code list • Ezines • Logon Scripts • PowerShell ErrorAction • Free Log Tool • Ezine 7 On Error Resume •Ezine 15 Error Codes • Ezine 33 Troubleshooting • Free WMI Monitor • Ezine 49 Errors • Ezine 54 Error correcting • Ezine 69 Option Explicit • Ezine 71 On Error |