9.12 Deciphering Script Errors

One of the general disadvantages of scripts is that they are typically created with a plain-text editor, rather than a rich debugging environment used with many more sophisticated programming languages (see Section 9.13 later in this chapter). Because Notepad isn't specifically designed to understand VBScript, it can't offer any assistance with syntax (grammar) or errors while you're editing. Therefore, you must wait until you run the script to see if there are any problems. If WSH encounters an error, it will display a message similar to that shown in Figure 9-2.

Figure 9-2. The Windows Script Host displays a message like this whenever it encounters an error.

figs/xpan_0902.gif

Surprisingly, this sparse message box actually provides enough information to resolve most problems. Naturally, the first field, Script, shows the script filename in which the error occurred. This is especially useful if the script was run from a scheduled task or from your Startup folder, and you might otherwise not know which script caused the error.

The Line Number and Column fields show exactly where in the script the error occurred, and include blank lines and remarks. If you're using Notepad, select Status Bar from the View menu to display the line number at which the insertion point (text cursor) is resting. Or, select Go To from Notepad's Edit menu to quickly jump to any line. Better yet, switch to a better text editor (discussed later in this chapter) that has more debugging tools, such as numbered lines.

The Category field describes more than anything else what it was doing when it encountered the error. A compilation error occurs when WSH is first reading the file and making sure all of the commands are correctly entered; you'll see this if you forgot a parenthesis or quotation mark, misspelled a command, or left out some other important keyword. A runtime error , on the other hand, is an error encountered while the script was being executed; this is caused by errors that WSH doesn't know are errors until it actually tries them, such as trying to read from a file that doesn't exist or trying to calculate the square root of a negative number.

Lastly, the Description field shows a brief explanation of the error encountered. Sometimes it's helpful, but most of the time it's either too vague or too cryptic to be of much help. This is where programming experience comes in handy for interpreting these messages and figuring out what caused them. The following are a few of the more common error descriptions and what they mean:

Expected `)'

Compilation error: you left out a closing parenthesis, such as at the end of an InputBox statement (see earlier). Note that sometimes you can have nested parenthesis (e.g., x=1+(6+7*(3-4))), and you need to make sure you have an equal number of open and close parentheses.

Expected `End'

Compilation error: you left out a closing statement for a structure, such as If, Sub, or For. Make sure you include End If, End Sub, and Next, respectively. Note that WSH might report that the error occurred on line 37 of a 35-line file; this happens because in looking for a closing statement, WSH continues to search all the way to the end of the script, at which time, if the statement was not found, it will report the error. You'll have to look through the entire script for the unpaired beginning statement. See the topics on flow control earlier in this chapter (Section 9.1.3, Section 9.1.4, and Section 9.1.5) for more information on these commands.

Unterminated string constant

Compilation error: you left out a closing quotation mark, usually required at the end of a "string of text."

Invalid procedure call or argument

Runtime error: this usually means that a subroutine or function has been called with one or more improper parameters. This can occur, for example, if you try to do something WSH isn't capable of, such as calculating the square root of a negative number.

Type mismatch: `[undefined]'

Runtime error: this means you've tried to use a command or function that VBScript doesn't recognize. You'll get this error whenever you try to use a VB command that doesn't exist in VBScript.

Object doesn't support this property or method

Runtime error: because it can be difficult to find documentation on the various objects used in VBScript, you're likely to encounter this error frequently. It means that you've tried to refer to a property or method of an object (such as WScript) that doesn't exist (such as WScript.Dingus).

The system cannot find the file specified

Runtime error: This error, obviously reporting that you've tried to access a file on your hard disk that doesn't exist, also appears when you try to delete a Registry key that doesn't exist. See Section 9.3 earlier in this chapter for a Registry function that solves this problem.

ActiveX component can't create object

Runtime error: you'll get this when you try to use the Set statement, as described throughout this chapter, and for whatever reason, WSH isn't familiar with the object you're trying to initialize. Typically, objects are extensions to WSH, some of which come with Windows XP, and some of which are installed through Add or Remove Programs, and some of which come with third-party programs. The resolution usually involves installing the missing component (which usually can be found on the Web), but depends entirely upon the specific object reported by the error.

If you plan on distributing your scripts, you'll want to take steps to eliminate any error messages that may pop up. See the Section 9.4 example script earlier in this chapter for more information on error trapping and the On Error Resume Next statement.



Windows XP Annoyances
Fixing Windows XP Annoyances
ISBN: 0596100531
EAN: 2147483647
Year: 2005
Pages: 78
Authors: David A. Karp

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