Understanding Error Types

To understand how debugging is useful, consider the three kinds of errors you can encounter:

Compile Errors

Compile errors result from incorrectly constructed code. If you incorrectly type a keyword, omit some necessary punctuation, or use a Next statement without a corresponding For statement at design time, Visual Basic detects these errors when you compile the application.

Compile errors include errors in syntax. For example, you could have a statement as follows:

Left$

Left is a valid word in the Visual Basic language, but whenyou add the invalid character $, it does not meet the syntax requirements for that word. If you have selected the Auto Syntax Check option in the Editor tab on the Options dialog box, Visual Basic will display an error message as soon as you enter a syntax error in the Code window. This option is on by default.

    To set the Auto Syntax Check option

  1. From the Tools menu, select Options, and click the Editor tab on the Options dialog box.
    1. Select Auto Syntax Check.

For more information on compile errors, see Compile-Time Errors.

Run-Time Errors

Run-time errors occur while the application is running when a statement attempts an operation that is impossible to carry out. An example of this is division by zero. Suppose you have this statement:

Speed = Miles / Hours

If the variable Hours contains zero, the division is an invalid operation, even though the statement itself is syntactically correct. The application must run before it can detect this error.

Logic Errors

Logic errors occur when an application doesn't perform the way it was intended. An application can have syntactically valid code, run without performing any invalid operations, and yet produce incorrect results. Only by testing the application and analyzing results can you verify that the application is performing correctly.

How Debugging Tools Help

Debugging tools are designed to help you with:

For instance, an incorrect result may be produced at the end of a long series of calculations. In debugging, the task is to determine what and where something went wrong. Perhaps you forgot to initialize a variable, chose the wrong operator, or used an incorrect formula.

There are no magic tricks to debugging, and there is no fixed sequence of steps that works every time. Basically, debugging helps you understand what's going on while your application runs. Debugging tools give you a snapshot of the current state of your application, including:

The better you understand how your application is working, the faster you can find bugs.