Clearing Errors

Microsoft® Windows® 2000 Scripting Guide

Line 14 of the script in Listing 2.14 uses the Clear method to explicitly reset the properties of the Err object. This is important, because otherwise these properties change only when a new error occurs. When no error occurs, the property values remain the same. This might cause your script to take inappropriate action based on the mistaken notion that another error has occurred.

Here is what can happen if you do not explicitly reset the properties of the Err object. When a script begins, the Err object has the default number 0 and empty source and description properties. If the script cannot connect to the computer atl-dc-01, the Err object properties are set as shown in Table 2.5.

This works as expected. However, what happens when the computer iterates through the loop and attempts to connect to atl-dc-02? In this case, the attempt succeeds, and no error is generated. However, the Err object still contains the property values shown in Table 2.5. Why? Because the Err object is not updated unless an error occurs. Next the script checks the error number and sees 462, the value left over from the failed attempt to connect to atl-dc-01. Because 462 is not equal to 0, the script takes action based on the incorrect assumption that an error occurred and atl-dc-02 must therefore be inaccessible. This same problem occurs when the script attempts to retrieve free disk space on atl-dc-03.

The Clear method overcomes this problem by resetting the properties of the Err object back to the default values. (Number is 0; source and description are empty.) Because the values have been reset, they will correctly reflect the fact that no error occurred when the script attempts to retrieve free disk space for atl-dc-02.