Flylib.com

Books Software

 
 
 

Understanding VBScript Errors


Understanding VBScript Errors

Errors can appear, even in small scripts, for a number of reasons. Errors may be generated when a script is first loaded or interpreted. Errors occurring at this stage are referred to as syntax errors . Syntax errors are often the result of typos. For example, you might accidentally type a single quote when you meant to type a closing double quote. Syntax errors also occur when you inadvertently mistype a VBScript keyword. Because syntax errors are discovered during the initial loading of a script, they are usually easily caught and corrected during script development.

start sidebar
Definition

A syntax error is an error that occurs as a result of not properly formatting statements within scripts.

end sidebar

Errors may also be generated during script execution. These types of errors are referred to as run-time errors . Run-time errors only appear when the statements that generate them are executed. As a result, some run-time errors may not be detected when the script executes (if the statement containing the error is not executed). For example, a run-time error may be hidden deep within a function or subroutine that is seldom called.

start sidebar
Definition

A run-time error is an error that occurs when a script tries to perform an illegal action, such as multiplying a numeric and a character value.

end sidebar

With proper testing of all the components of a script, most run-time errors can be discovered and fixed during script development. I say "most" because not all runtime errors can be caught—those caused by unforeseen circumstances may be impossible to detect during script development. Perhaps the person running the script incorrectly supplied input in a manner that you could not have anticipated, or perhaps something is wrong with the environment in which the script is being executed. Or maybe the hard disk has become full, preventing your VBScript from writing to a file, or the network goes down as your script is executing a file copy or move operation. In cases such as these, often the best you can do is to end the script gracefully, without confusing the user or making the situation worse .

start sidebar
Definition

A logical error is an error produced as a result of a programming mistake on the part of the script developer.

end sidebar

Another category of error to which scripts are susceptible is logical errors. Logical errors are mistakes made by the script developer. For example, instead of looping ten times, you might accidentally set up an endless loop. Another example of a logical error is a situation in which a script adds two numbers that should have been multiplied.

Understanding Error Messages

VBScript error messages are generated for both syntax and run-time errors. These errors are displayed in the form of popup dialogs, as demonstrated in Figure 8.6. Each error message displays information about the error, including a brief description of the error, an error number, and the source of the error.

click to expand
Figure 8.6: A typical VBScript error message

TRICK 

Each VBScript syntax and run-time error is assigned an error number. Depending on the execution host, this number may be displayed either as a decimal number or a hexadecimal number. Error messages produced by VBScripts processed by the WSH are displayed with a hexadecimal number. Microsoft's VBScript error message documentation, which you will find at www.msdn.microsoft.com/scripting, only lists VBScript's error messages by their decimal numbers.

You can translate between a VBScript error's hexadecimal and decimal number. First, drop the 800A portion of the message. Then open the Windows Calculator application in scientific mode, select the hexadecimal setting and type the last four digits of the hexadecimal number. Select the decimal setting, and the calculator will show the decimal equivalent. In Figure 8.6, 800A03F9 is the hexadecimal equivalent to the decimal number 1017. If this seems like too much work, just refer to this chapter's Tables 8.1 and 8.2, which list VBScript syntax and run-time errors, and you'll see that I've already done the math for you.

Table 8.1: VBSCRIPT SYNTAX ERRORS

Hexadecimal

Decimal

Description

800A03E9

1001

Out of Memory

800A03EA

1002

Syntax error

800A03ED

1005

Expected ' ('

800A03EE

1006

Expected ') '

800A03F2

1010

Expected identifier

800A03F3

1011

Expected '='

800A03F4

1012

Expected 'If'

800A03F5

1013

Expected 'To'

800A03F5

1013

Invalid number

800A03F6

1014

Expected 'End'

800A03F6

1014

Invalid character

800A03F7

1015

Expected 'Function'

800A03F7

1015

Unterminated string constant

800A03F8

1016

Expected 'Sub'

800A03F9

1017

Expected 'Then'

800A03FA

1018

Expected 'Wend'

800A03FB

1019

Expected 'Loop'

800A03FC

1020

Expected 'Next'

800A03FD

1021

Expected 'Case'

800A03FE

1022

Expected 'Select'

800A03FF

1023

Expected expression

800A0400

1024

Expected statement

800A0401

1025

Expected end of statement

800A0402

1026

Expected integer constant

800A0403

1027

Expected 'While' or 'Until'

800A0404

1028

Expected 'While', 'Until', or end of statement

800A0405

1029

Expected 'With'

800A0406

1030

Identifier too long

800A040D

1037

Invalid use of 'Me' keyword

800A040E

1038

'loop' without 'do'

800A040F

1039

Invalid 'exit' statement

800A0410

1040

Invalid 'for' loop control variable

800A0411

1041

Name redefined

800A0412

1042

Must be first statement on the line

800A0414

1044

Cannot use parentheses when calling a Sub

800A0415

1045

Expected literal constant

800A0416

1046

Expected 'In'

800A0417

1047

Expected 'Class'

800A0418

1048

Must be defined inside a class

800A0419

1049

Expected Let or Set or Get in property declaration

800A041A

1050

Expected 'Property'

800A041B

1051

Number of arguments must be consistent across properties specification

800A041C

1052

Cannot have multiple default property/method in a class

800A041D

1053

Class initialize or terminate do not have arguments

800A041E

1054

Property Set or Let must have at least one argument

800A041F

1055

Unexpected Next

800A0421

1057

'Default' specification must also specify 'Public'

800A0422

1058

'Default' specification can only be on Property Get

Table 8.2: VBSCRIPT RUN-TIME ERRORS

Hexadecimal

Decimal

Description

800A0005

5

Invalid procedure call or argument Overflow. Out of Memory

800A0006

6

Overflow

800A0007

7

Out of Memory

800A0009

9

Subscript out of range

800A000A

10

This array is fixed or temporarily locked

800A000B

11

Division by zero

800A000D

13

Type mismatch

800A000E

14

Out of string space

800A0011

17

Can't perform requested operation

800A001C

28

Out of stack space

800A0023

35

Sub or function not defined

800A0030

48

Error in loading DLL

800A0033

51

Internal error

800A005B

91

Object variable not set

800A005C

92

For loop not initialized

800A005E

94

Invalid use of Null

800A01A8

424

Object required

800A01AD

429

ActiveX component can't create object

800A01AE

430

Class doesn't support Automation

800A01B0

432

Filename or class name not found during Automation operation

800A01B6

438

Object doesn't support this property or method

800A01BD

445

Object doesn't support this action

800A01BF

447

Object doesn't support current locale setting

800A01C0

448

Named argument not found

800A01C1

449

Argument not optional

800A01C2

450

Wrong number of arguments or invalid property assignment

800A01C3

451

Object not a collection

800A01CA

458

Variable uses an Automation type not supported in VBScript

800A01CE

462

The remote server machine does not exist or is unavailable

800A01E1

481

Invalid picture

800A01F4

500

Variable is undefined

800A01F6

502

Object not safe for scripting

800A01F7

503

Object not safe for initializing

800A01F8

504

Object not safe for creating

800A01F9

505

Invalid or unqualified reference

800A01FA

506

Class not defined

800A01FB

507

An exception occurred

800A1390

5008

Illegal assignment

800A1399

5017

Syntax error in regular expression

800A139A

5018

Unexpected quantifier

800A139B

5019

Expected ] in regular expression

800A139C

5020

Expected) in regular expression

800A139D

5021

Invalid range in character set

The error message displayed in Figure 8.6 is the result of a syntax error. As you can see, a lot of useful information about the error is automatically provided. The ability to interpret and understand this information is critical for trouble-shooting and fixing your VBScripts.

The following information has been provided in this error message:

  • Script. The name and location of the VBScript that produced the error.

  • Line. The line number within the VBScript where the error was detected.

  • Char. The column number position within the line where the error was detected.

  • Error. A brief description of the error.

  • Code. An error number identifying the type of error.

  • Source. The resource that reported the error.

You can see in Figure 8.6 that a VBScript named X.VBS located in C:\Temp generated the error. Line 6 of the script that generated this error looks like the following statement:

If X > 5 MsgBox "Hello World!"

This If statement uses the VBScript MsgBox() function to display a text string. The error message indicates that the problem is that VBScript expected to find the 'Then' keyword and did not. If you take a look at the middle of this statement you'll see that, in fact, the Then keyword is absent. To correct this error, you would add the missing keyword, like this:

If X > 5 Then MsgBox "Hello World!"

To verify that the error has been eliminated, you could then save and run the script again.

Fixing Syntax Errors

VBScripts are subject to many different types of syntax errors, which Microsoft documents as part of the VBScript documentation available at http://www.msdn.microsoft.com/scripting. For your convenience, I've provided this information in Table 8.1. In addition, I listed both the decimal and hexadecimal error number associated with each error.

Catching Run-Time Errors

VBScripts are also subject to a wide range of possible run-time errors. Microsoft documents these errors as part of their VBScript documentation, which is available at http://msdn.microsoft.com/scripting. For your convenience, I've provided this information in Table 8.2. In addition, I listed both the decimal and hexadecimal error numbers for each run-time error.

Preventing Logical Errors

Your VBScripts will do exactly what you tell them to do, even if that's not what you really mean for them to do. Therefore, it's extremely important that you plan your VBScript project carefully . For example, you should begin with a pseudo code outline and then translate that into a flowchart, outlining each of the script's major components. Then you should, as much as possible, develop the script a component at a time, testing each component as you go. I'll show you some different ways to test individual script components as you develop the Hangman game.

Logical errors often make their presence known by presenting incorrect or unexpected results, and can be the most difficult type of error to track down. Unlike syntax and run-time errors, which display messages that describe the nature of their problems, logical errors force you to look through some or all of your script, a line at a time in order to find the faulty logic. The good news is that with careful planning and design, logical errors can be avoided.