Handling Run-Time Errors
Microsoft® Windows® 2000 Scripting Guide
For the most part, to handle a run-time error simply means, "If an error occurs, do not allow the script to fail, but do something else instead." Typically the "something else" that you can do is one of the following:
You can ignore the error and proceed with the next line of the script.
You can trap the error and include code that responds to the error in some way (for example, by displaying an error message to the user).
Both of these methods for handling errors are implemented by including the On Error Resume Next statement within the script. When running under On Error Resume Next, a script will not fail when encountering a run-time error. Instead, the script will ignore the line of code where the error occurred and will attempt to process the next line of code. This will continue until every line of code has been either ignored (because an error occurred) or processed.
Ignoring All Errors
By far the simplest form of error handling is the type that instructs a script to ignore all errors and continue running until every line of code has been processed. To create a script that ignores all errors and continues to run, place the On Error Resume Next statement at the beginning of the script. For example, the following script attempts to use a nonexistent WSH method (Wscript.X); none of the lines of code can thus run successfully. However, the script will actually run without generating an error message because of the On Error Resume Next statement:
On Error Resume Next Wscript.X "Testing 1-2-3." Wscript.X "Testing 4-5-6" Wscript.X "Testing 7-8-9"
Note that error handling does not begin until VBScript processes the On Error Resume Next statement. For example, the following script will generate a run-time error because On Error Resume Next is not implemented until the error has already occurred:
Wscript.Echo "Line 1." Wscript.Echo "Line 2." Wscript.Eho "Line 3." On Error Resume Next Wscript.Echo "Line 4."
To ensure that error handling is in place before an error occurs, put the On Error Resume Next statement near the beginning of your script:
On Error Resume Next Wscript.Echo "Line 1." Wscript.Echo "Line 2." Wscript.Eho "Line 3." Wscript.Echo "Line 4."
When the preceding script is run under CScript, the following output appears in the command window. When the script host encountered the run-time error generated by Wscript.Eho, it simply skipped that line and continued with the rest of the script:
Line 1. Line 2. Line 4.
Responding to Errors
Instead of having your script ignore errors, you can create code that periodically checks the error condition and then takes some sort of action. For this purpose, you might create code that:
Checks to see whether the value of the Err object is not equal to 0. (If Err is equal to 0, that means that no run-time error has occurred.)
Takes some sort of action (for example, displaying the value of Err.Number and Err.Description).
Resets the value of the Err object to 0. This is very important because, as noted previously in this chapter, the Err object does not automatically reset itself each time the error condition is checked.
For example, the following script sample first attempts to connect to the WMI service. If this connection attempt fails (that is, if Err does not equal 0), a message box is displayed showing the error number and description. After displaying the error message, the script then clears the Err object.
The script then attempts to return a list of all the services installed on a computer. This line of code will fail because the ExecQuery method has been misspelled as ExcQuery. After running this line of code, the script will again check the error condition. If Err does not equal 0, a message box will be displayed showing the error number and description. After displaying the error message, the script then clears the Err object.
On Error Resume Next Set Computer = GetObject("winmgmts:") If Err <> 0 Then Wscript.Echo Err.Number & " -- " & Err.Description Err.Clear End If Set ServiceList = Computer.ExcQuery("SELECT * FROM Win32_Service") If Err <> 0 Then Wscript.Echo Err.Number & " -- " & Err.Description Err.Clear End If
When this script is run under WScript, the message box in Figure 2.22 is displayed:
Figure 2.22 WMI Error Message