Export (0) Print
Expand All

Introducing Error Handling

Microsoft® Windows® 2000 Scripting Guide

VBScript provides a relatively simple method for handling run-time errors within a script. Run-time errors represent any errors that are not caught by the compiler and thus manifest themselves only after the script has started running. For example, the following script generates a syntax error because the command Wscript.Echo is mistyped and the compiler is unable to interpret the line. Before a script actually runs, the scripting engine reads each line to verify that the syntax is correct; no lines of code are actually run until this checking verifies that the syntax of all the lines of code is correct. Because of that, an error message will be displayed, even though the misspelled command does not occur until line 3 in the script:



Wscript.Echo "Line 1."
Wscript.Echo "Line 2."
W script.Echo "Line 3."

Instead of the message boxes for lines 1 and 2, the error message shown in Figure 2.20 appears. This error is generated because line 3 appears to reference an invalid command named W.

Figure 2.20 Syntax Error Message

sas_vbs_21s

By contrast, the following script will display two message boxes before encountering an error (the misspelling of Wscript.Echo). This is because, as far as the compiler is concerned, line 3 is typed correctly. The compiler has no way of knowing that Eho is not a valid Wscript property and thus does not flag this as an error. VBScript cannot determine the properties and methods of a COM object in advance (that is, before a particular line of code runs).



Wscript.Echo "Line 1."
Wscript.Echo "Line 2."
Wscript.Eho "Line 3."

When line 3 of the script is actually run, the error message shown in Figure 2.21 appears. You might notice that this error message lists the source as a Microsoft VBScript run-time error rather than as a compilation error.

Figure 2.21 Runtime Error Message

sas_vbs_22s

Not all run-time errors involve typographical errors within the script. For example, this line of code is syntactically and typographically correct. However, it will also generate a run-time error if the remote computer named InaccessibleComputer is not available over the network:



Set Computer = GetObject("winmgmts:\\InaccessibleComputer")

This means that run-time errors are bound to occur, if only because of activities beyond your control. (The network goes down, a computer is shut down, another administrator deletes a user account.) With VBScript, you have three options for handling errors. The advantages and disadvantages of each of these options are summarized in Table 2.19.

Table 2.19 Advantages and Disadvantages of Error Handling Options

Option

Advantages

Disadvantages

Allow the script to fail.

  • Requires no work of any kind on the part of the script writer. By default, all scripts will fail when they encounter a run-time error.

  • In many cases, running part of a script might be worse than not running a script at all. For example, if you have a script that backs up files to a remote computer and then deletes the original files, you would probably prefer that the script fail if the backup cannot take place. You would not want the backup portion to fail but then have the delete portion succeed.

  • Allowing the script to fail at some arbitrary point could leave your computer in an unstable state.

  • Some failures need not be fatal. For example, suppose a monitoring script is designed to retrieve information from 100 computers. If computer 1 is inaccessible, the script will fail and no information will be returned, even though the other 99 computers are up and running.

Have the script ignore all errors.

  • Scripts will continue to run without interruption.

  • Scripts can complete a major share of their tasks, even if a problem arises. For example, a script might be able to successfully copy 99 of 100 files, even if one file could not be copied.

  • End users will not be presented with error messages that they must respond to.

  • Difficult to debug problems because no message is displayed indicating that an error occurred, nor is any clue given as to where the error might have taken place.

  • Can leave a computer in an uncertain state, wherein some actions have been carried out while others have not.

Write code to respond to errors as they occur.

  • Allows you to create meaningful error messages.

  • Allows you to create code that attempts to address the error that occurred. For example, if a script is unable to connect to a remote computer, it might try to make that connection again before terminating.

  • Requires extra work on the part of the script writer. The script writer must anticipate where errors are likely to occur and then include the code to respond appropriately.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft