Java Reference
In-Depth Information
C H A P T E R 1 1
Debugging with Eclipse
Not every error throws an exception; sometimes, due to poor programming, your code will do something
totally wrong without throwing an exception. The computer did exactly what you told it to do, but it
didn't do what you meant. When programming, it pays to remember that a computer is just a
construction of silicon and various metals, so it is literally as dumb as a box of rocks. It has no conscious
ability to adjust for your errors, as another person does when you speak or write.
Software developers love exceptions. Every time Java throws an exception, it provides a stack trace,
showing us the classes and methods (and line numbers therein) that led to the exception. That makes
finding and fixing the error easy.
In the absence of an exception, though, you have to either puzzle it out by examining the logic of the
program or use a debugger. Sometimes, just examining the logic will do the trick. Other times, however,
only a debugger will do the job. As my editors could surely tell you, I often miss my own errors. I know
what I meant to write and look past what I actually wrote. The same problem applies to programming:
we know what we intended to do and often overlook what we actually did. A debugger forces you to look
at your code differently, which can help you find the problem. Also, a debugger “steps” through the code
one line at a time. That narrow scope (a single line) allows you to really focus to find any error.
Eclipse includes a full-featured debugger, and the rest of this chapter describes how to use that
debugger to track down and fix a problem. Other debuggers exist for Java, and Java includes a
command-line debugger called JDB (The Java Debugger). However, I think the Eclipse debugger is easier
to use and provides all the features one needs. For what it's worth, I generally use it for my own work.
I had a problem with the Fireworks program from Chapter 10, “Animation” that I had to use the
debugger to find, so I'll use that program as an example for our debugging exercise. In fact, I'll start by
showing you a problem that I used the Eclipse debugger to solve. Figure 11-1 shows the mess I made of
the Fireworks program before I got it to work correctly.
Search WWH ::




Custom Search