Java Reference
In-Depth Information
Run the program again; type in exactly the same answers, and click with the mouse
on the same spots (or as close as you can get). Does the program exhibit the same
behavior? If so, then it makes sense to fire up a debugger to study this particular
problem. Debuggers are good for analyzing particular failures. They aren't terribly
useful for studying a program in general.
Step 2 Simplify the error.
Before you fire up a debugger, it makes sense to spend a few minutes trying to
come up with a simpler input that also produces an error. Can you use shorter
words or simpler numbers and still have the program misbehave? If so, use those
values during your debugging session.
Step 3 Divide and conquer.
Now that you have a particular failure, you want to get as close to the failure as
possible. The key point of debugging is to locate the code that produces the failure.
Just as with real insect pests, finding the bug can be hard, but once you find it,
squashing it is usually the easy part. Suppose your program dies with a division by
0. Because there are many division operations in a typical program, it is often not
feasible to set breakpoints to all of them. Instead, use a technique of divide and
conquer. Step over the methods in main , but don't step inside them. Eventually,
the failure will happen again. Now you know which method contains the bug: It is
the last method that was called from main before the program died. Restart the
debugger and go back to that line in main , then step inside that method. Repeat
the process.
Use the divide-and-conquer technique to locate the point of failure of a
program.
Eventually, you will have pinpointed the line that contains the bad division. Maybe
it is completely obvious from the code why the denominator is not correct. If not,
you need to find the location where it is computed. Unfortunately, you can't go
back in the debugger. You need to restart the program and move to the point where
the denominator computation happens.
Step 4 Know what your program should do.
Search WWH ::




Custom Search