Using the Err Object

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

On Error Resume Next allows your script to continue to function should a run-time error occur. There are at least two potential problems with this, however. For one thing, no error message is generated to let you know that an error occurred, so you will have no way of knowing where the script failed.

For another, you might prefer that a script not attempt to run every single line in the event of a run-time error. For example, consider a script that does the following:

  1. Connects to a remote computer.
  2. Copies a set of files from the local computer to the remote computer.
  3. Deletes the original set of files from the local computer.

Suppose you run this script and the remote computer is not available. Here is what could happen.

  1. The script attempts to connect to the remote computer and fails. However, On Error Resume Next ensures that the script continues to run.
  2. The script attempts to copy files to the remote computer. This fails because the remote computer is not accessible.
  3. The script deletes the files from the local computer. Unfortunately, this succeeds because the local computer is available. As a result, the files are deleted from the local computer but are not copied to the remote computer.

Fortunately, you can use the intrinsic VBScript Err object to determine whether an error occurred and, if so, take the appropriate action.

The Err object is automatically created each time you run a script. (There is only one Err object per script instance.) The Err object includes several properties, including the three shown in Table 2.4. Whenever your script encounters a run-time error, these properties are automatically filled in with information that identifies the error.

Table 2.4   Err Object Properties

PropertyDescription
DescriptionDescription of the error. This can be used to inform the user that an error has occurred simply by echoing the value:
Wscript.Echo Err.Description 
NumberInteger uniquely identifying the error that occurred. The number might represent an intrinsic VBScript error number, or it might represent an error number derived from an Automation object. To determine where the error number came from, use the Source property.
SourceClass name or programmatic identifier (ProgID) of the object that caused the error. If VBScript caused the error, you typically see "Microsoft VBScript runtime error" as the source. If an Automation object is responsible for the error, you see the ProgID (for example, "Word.Application").

When a script is started, VBScript assigns the default value 0 (no error) to the Number property. If the script encounters an error, the value of the Number property will change accordingly. This enables you to periodically check to see whether your script has encountered any errors. For example, you might want your script to check error status after it attempts to connect to a remote computer. If Err.Number equals 0 (zero), no error occurred. If Err.Number does not equal 0, an error of some kind has occurred, and you can assume that the attempt to connect to the remote computer failed. As a result, your script can take action based on the fact that the remote computer was unavailable.

This type of error handling is implemented in the script in Listing 2.14. In line 1 of the script, On Error Resume Next enables error handling. In line 10, the script sets up a For Each loop to cycle through a list of server names. In line 11, the script attempts to connect, in turn, to each of these servers.

But what happens if one of these servers is not accessible? In line 11, the script tries to connect to one of these remote servers. If the connection is successful, no error is generated, meaning that Err.Number will remain 0. If the connection fails, however, an error will be generated and Err.Number will be changed to reflect the number corresponding to that error.

If the connection attempt in line 11 fails, On Error Resume Next ensures that the script next tries to run line 12. In line 12, the script checks the value of Err.Number. If the value is anything but 0 (meaning that an error occurred), the script echoes Err.Description and then restarts the loop with the next server name. If the value is 0, this means that the connection succeeded. The script will then retrieve the free disk space on the computer in question.

Listing 2.14   Handling Errors

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
On Error Resume Next Const CONVERSION_FACTOR = 1048576 Const WARNING_THRESHOLD = 100 If WScript.Arguments.Count = 0 Then     Wscript.Echo "Usage: FirstScript.vbs server1 [server2] [server3] ..."     WScript.Quit End If For Each Computer In WScript.Arguments     Set objWMIService = GetObject("winmgmts://" & Computer)     If Err.Number <> 0 Then         Wscript.Echo Computer & " " & Err.Description         Err.Clear     Else         Set colLogicalDisk = _             objWMIService.InstancesOf("Win32_LogicalDisk")         For Each objLogicalDisk In colLogicalDisk             FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR             If FreeMegaBytes < WARNING_THRESHOLD Then                 Wscript.Echo Computer & " " & objLogicalDisk.DeviceID & _                     " is low on disk space."             End If         Next     End If Next

If you run the script in Listing 2.14 and one of the servers is not accessible, the properties of the Err object will be populated as shown in Table 2.5.

Table 2.5   Values Assigned to Err Object Properties

PropertyValue
Err.DescriptionThe remote server machine does not exist or is unavailable
Err.Number462
Err.SourceMicrosoft VBScript run-time error

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net