Debugging Logical Errors

Logical errors are more difficult to debug mainly because it isn't obvious where the error is occurring. Your first task then is to root out the source of the error. To do this, consider all of the variables that may play a part in the production of erroneous output. Although complex procedures may utilize many different variables, usually, as the author of the program, you'll have a good idea as to the one or two to focus on.

Figure 4.5

The Locals Window can validate that you initialized a variable correctly but are not using the object in a manner in which it was intended.

Figure 4.5

The Locals Window can validate that you initialized a variable correctly but are not using the object in a manner in which it was intended.

Next, consider whether the error occurs for all inputs or if it only occurs with a few specific inputs. If it occurs for all inputs, it is easier to debug because you can just step through the program in Break mode and observe the variables in question. An even more efficient method is to set a Breakpoint at some point in the procedure in question so you can zip right to the appropriate section in your code without having to step through a bunch of code that is irrelevant to the issue at hand. If the error only occurs for specific inputs, create a Watch that will place you in Break mode when the variable assumes a value that produces an error.

Finally, once you locate the source of the error, you can develop an appropriate solution to the problem like you would with any other error. Ninety percent of the work with logical errors is determining where they are occurring.

0 0

Post a comment