This tutorial follows on from our Testing tutorial. Debugging locates and corrects program defects detected by testing. We recommend Ian Lance Taylor's authoritative essay on debugging. Ian's advice is as appropriate for Pascal as it is for other languages. We reproduce here some questions that you should address when debugging:
  • Can you replicate the bug?
  • What caused the bug?
  • Which part of the program caused the bug?
  • Does the same programming error occur anywhere else in the program?
  • What new problems might be introduced by the bug fix?

We describe in our Common Error Checklist some examples of syntax errors, run-time errors and logic errors. An awareness of these common errors can save a lot of time when debugging. We hope you have tried our Programs to Debug. These should be possible with careful inspection of the code or, failing that, using the error messages received when you try to compile them. This tutorial covers a range of methods for more difficult debugging.

Most of the methods rely on stepping through the code, usually examining values of variables. The first section describes how to step through the code from the start of the program and how to execute a subroutine without stepping through it. The second section shows two methods you can use to examine variables.

Usually it is inconvenient to step through the code from the start of the program. We describe in the third section how to set breakpoints and how to set their properties so that they can cause the program to stop after passing through a line of code a set number of times or when a certain condition is met.

Sometimes programs can fail without giving useful information about the cause of the fault. We cover several different methods that you can use to improve matters. It can be useful to include in debug versions of the program lines of code that output helpful messages. You can use writeln statements in console programs, but the OutputDebugString procedure described in Outputting Messages offers advantages. We describe in the Error Handling section the use of try ... except code where the program will output your own diagnostic message when a runtime error is encountered. The Range Checking section shows how the use of a compiler directive can cause the generation of useful troubleshooting information.

You may introduce errors when you copy then edit code. In the Replacing Text section we describe common errors resulting from copying and show the potential of regular expressions for achieving effective copying.

Follow these links to the sections described above.

Programming - a skill for life!

Pascal Programming Tutorials