Step by step program debugging

Debugging programs

Any sufficiently large program requires debugging, which consists in finding and eliminating errors. The integrated development environment contains all the necessary tools to eliminate any type of errors and control the progress of the program. It provides tools that allow you to execute programs in step-by-step mode, monitor changes in the values of variables or expressions, and trace (log calls) to procedures.

Various types of errors can be present in the program you are developing. There are usually three types of errors:

1. Compilation errors. Such errors occur in incorrectly compiled program constructs. Examples of such errors include incomplete statement pairs (such as If…End If or For…Next) or errors that violate Visual Basic language rules (such as misspelled keywords, missing delimiters, or invalid data types). Compilation errors also include syntax errors that result from violations of grammar or punctuation rules. Examples of this type of error are incomplete pairs of parentheses or an incorrect number of arguments passed to a function.

2. Errors during execution occur already at the stage of program execution. This type of error includes, for example, illegal operations, the most famous of which is division by zero.

3. Logical errors are called errors that do not interfere with the execution of the program, but lead to incorrect results. For example, when writing a program, we can enter a minus instead of a plus into the expression. In this case, the program may work, but the result will, of course, be far from expected.

Errors of the first type are usually detected at the compilation stage or at the stage of writing the text of the program. The Visual Basic Editor automatically checks the syntax of a statement after pressing the Enter key, and displays an error message if an error occurs.

Errors of the second and third types can be eliminated by debugging the program in step-by-step mode.

Debugging a program in step mode

Execution of the program in step-by-step mode is designed to localize the place where the error occurs. Using this mode allows you to see the results of executing each line of the program.

Interrupt mode

To start debugging a program in step-by-step mode, you must first pause its execution. In this case, the program itself remains loaded, but it stops before executing the next instruction. This mode of operation of the integrated development environment is called break mode.

NOTE : The IDE can be in one of the following modes: form design mode (forms are created in this mode), run mode (the transition to it is carried out immediately after the program starts), interrupt mode (when the program execution is suspended, we switch to interrupt mode, allowing you to debug programs)

To interrupt program execution, you can use one of the following methods:

· Add a Stop statement to the procedure.

· Execute the Run→Break command during program execution. In this case, the line of the program on which its execution was interrupted will be displayed.

· Set a breakpoint.

· Run the program not through the Run menu commands, but using the Debug menu commands (see the Debug Menu section).

Program execution will also be aborted if a runtime error occurs. In this case, a dialog box will appear (Fig. 6.1), in which the error code is given and the physical interpretation of its occurrence is given.

Fig 6.1 Dialog window with error message

Pressing the Debug button in this window causes the program to be interrupted and the Visual Basic editor window is called, in which the cursor will be placed on the line of code with the detected error.

Setting a breakpoint

A breakpoint is a program line at which program execution is automatically paused. To set a breakpoint, do the following:

1. Enter the program window and place the cursor on the line where you want to stop the program.

2. Click the Toggle Breakpoint button on the Debug toolbar or run the Debug→Toggle Breakpoint command.

After executing the command Toggle Breakpoint (Breakpoint), the line with the set breakpoint will be marked with a certain color. At the same time, a special icon will appear on the indicator strip, in the field opposite the selected line, as shown in Fig. 6.2.

Fig 6.2 Setting a breakpoint

NOTE: When debugging programs, the area along the left edge of the program window is often used. This area is called the Margin Indicator Bar. Its fields display various indicators used when debugging programs.

TIP: You can also set a breakpoint in another, faster way: click the mouse button in the indicator bar field opposite the line where you want to set a breakpoint.

Removing a breakpoint

To remove a breakpoint, move the cursor to the line where the breakpoint is set and click the Toggle Breakpoint button on the Debug toolbar (Breakpoint). To continue running the program, select the command Run→Run Sub/UserForm (Run→Continue). You can also remove a breakpoint by clicking in the indicator bar area opposite the desired line. You can also clear all breakpoints with the Debug→Clear All Breakpoints command (Debug→Clear All Breakpoints).

Step by step program debugging

Step-by-step debugging of the program can be done in different modes:

1. If you need to implement the sequential execution of each line of the program, including the lines of all called procedures, click the Step Into button on the Debug toolbar, or the command of the same name on the Debug menu, or the F8 key.

NOTE : To use the Step Into command, it is not necessary to start and pause the program. It is enough to place the cursor in the program window on the procedure that you want to debug in step-by-step mode, and execute the Step Into command.

2. If you don’t want to step into called procedures, click the Step Over button on the Debug toolbar. You can use the Debug menu command of the same name or the Shift+F8 key combination. As a result, the called procedures will be executed without stepping through them.

3. To exit the procedure, you can use the button or the Step Out command. , and also press Ctrl+Shift+F8. In this case, the current procedure will be executed to the end and the program will jump to the line following its call.

4. If you want to execute a part of the program up to the line where the cursor is located, select the command Debug→Run To Cursor (Debugging→Run to the current position) or press Ctrl+F8. In this case, the program will be executed up to the current line and there will be a transition to the step-by-step mode.

Be First to Comment

Leave a Reply

Your email address will not be published.