Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Kinds of Errors

You already know that a bug is a program error. Several kinds of bugs exist, however. The kind of error that appears determines how you will fix the bug.

A syntax error is a bug that appears because you misspelled a command or used improper grammar.

Syntax errors are the easiest errors to remove from your program because Visual Basic finds them for you. Take a moment to display the Options dialog box shown in Figure 20.1. (Select Tools | Options to see this dialog box.)

20fig01.gif

Figure 20.1 Letting Visual Basic find syntax errors for you.

The option labeled Auto Syntax Check turns on and off Visual Basic's automatic detection of syntax errors as you type them. In other words, if the option is set, and you type a statement with a syntax error, the Code window will look over your shoulder and inform you immediately of the error.

Notice what happens in Figure 20.2. The programmer was trying to enter this statement:

20fig02.gif

Figure 20.2 Visual Basic detected the syntax error.

Private Functiion CalcTotals(x As Integer) As Double

Although it's fairly obvious that the programmer misspelled Function, and it's obvious that the Code window noticed something wrong, here are two things to notice about this automatic syntax check:

  • The error message box reads Compile error, not Syntax error.
  • Visual Basic highlighted the wrong word! Instead of highlighting the problem word Functiion, Visual Basic highlighted CalcTotals, the name of the function that has no problems.

The error message that appears rarely reads Syntax error because several kinds of syntax errors can occur. The error message Compiler error is less informative than some of the others, but the actual error is secondary to the fact that you typed something incorrectly. Perhaps you misspelled a word (true in this case), left off a quotation mark or a right parenthesis, forgot a built-in function argument list, or failed to end the statement with an underscore when you meant to continue the statement on the next line. When typing code and such an error message box appears, look back at the statement to find the error.

Often, but not always, Visual Basic will highlight the offending part of the statement. In this case, however, Visual Basic failed to locate the exact error. Instead, Visual Basic highlighted the first word found after the error. Visual Basic cannot always detect the exact location of the syntax error because it often has to interpret more of the statement before a problem becomes obvious. Therefore, if you don't see a problem with the current highlighted word, look back a word or two, and you'll find the mistake.

As you can see, syntax errors are the easiest errors to find. They either show up as you type the program code, or if you've turned off the automatic syntax error check, the syntax errors show up when you try to run or compile the program. Another kind of error, a runtime error, will not show up until you execute the program.

A runtime error shows up during the program's execution.

For example, suppose you are calculating an average salary figure, but you make a mistake in the calculation and attempt to divide the total by a variable with zero in it. Division by zero is undefined in mathematics (undefined for the real number system, to be exact), but Visual Basic cannot, when you write the code, know what value the variable will hold. Therefore, only at the time of execution when the division is about to take place can Visual Basic recognize that the division is impossible. Visual Basic will display a runtime error message box such as the one shown in Figure 20.3.

20fig03.gif

Figure 20.3 A runtime error occurred.

When you're faced with a runtime error, the dialog box gives you these choices:

  • Continue
  • End
  • Debug
  • Help

The Continue command button is rarely available because of the severity of most runtime errors. However, with the built-in debugging tools that Visual Basic makes available when you click Debug, you can possibly fix the problem and then continue with the program by clicking Continue. You'll learn all about the debugger in the next section.

If you click End, the program will stop and you'll return to the Code window, where you can locate and fix the problem if you don't need help from the debugging tools. If you want to read more information on the error message itself, click Help to display online help. Figure 20.4 shows the online help that appears when you click Help on the divide-by-zero error.

20fig04.gif

Figure 20.4 Visual Basic explains the error message.

Sometimes you tell the computer to do something that is wrong. The computer understands your instructions because no syntax or runtime errors appear, but the computer simply doesn't do what you want it to. In those cases, you've programmed a logic error.

A logic error produces undesired program results, but no error messages appear.

Logic errors are the most difficult to locate. Whereas the Code window tells you where a syntax error appeared, and the runtime system tells you when a runtime error appears, you must spot logic errors yourself (hopefully before your application's users spot them) and trace the problem to the source. The development environment's integrated debugger is the most useful tool for finding logic errors.

+ Share This