Home CPSC 240

Debugging

 

Overview

"Debugging" refers to the process of finding and fixing errors, or "bugs" in a program. The term comes from Admiral Grace Hopper who, while working on an early computer in the 1940s found a moth stuck in the computer. Now bugs are rarely caused by actual insects, but by errors in our code.

No program (except perhaps the most trivial) ever works correctly the first time. Even experienced programmers write code with bugs, and for this reason debugging skills are just as important as coding skills.

Note that debugging is not necessarily the same thing as using a debugger. A debugger is one tool for debugging, but you can also debug your program just by inserting print statements in it.


 

General Techniques


 

Debugging with IntelliJ

The first step in using IntelliJ's debugger is to create a break point. This is a line of code that the debugger will stop at when it gets to it. To make a break point, you can click on the column to the right of your code, on the line you want to stop at:

Setting a break point

You can then run your program with the debugger by choosing "Debug" from the Run menu. Your program will then stop when it gets to one of your breakpoints. You can then inspect the values of different variables. The debug window contains a view of the stack. You can click on this to see variables in your stack frames:

The debug stack window

Once we are at a break point like this, we can then step through the code using the following commands:

The debugger removes the need to add lots of print statements to your code. You can run through it and see the values of all your variables as the program runs.

Copyright © 2022 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.