Java Reference
In-Depth Information
Syntax errors
Syntax errors are the bugs you're probably most familiar with. We bet the first pro-
gram you ever wrote consisted primarily of syntax errors. (Heck, we still work with
some guys like that.) Syntax errors are invalid statements or code structure that the
Java compiler can't make sense of. In other words, they're just plain wrong.
Examples of syntax errors include referencing a variable that doesn't exist or
passing the wrong number of parameters to a method. Thanks to the real-time
syntax analysis feature of IDEA 's editor, you'll probably catch most of these errors
before they get to the compiler, but occasionally one will slip through.
Bugs of this type surface during compilation and therefore can't affect running
code. The debugger is designed for analyzing programs that run, and it can't help
fix compilation problems like syntax errors—you're on your own for that. It will,
however, help determine the cause of any runtime errors you encounter.
Runtime errors
Runtime errors stick out like a sore thumb; you can't miss them. They can rear
their ugly heads at any time, often with dramatic explosions of stack traces and
cryptic error messages. The JVM throws a runtime error when something unex-
pected happens, like trying to read off the end of an array or encountering a null
where an object was expected. In Java, these issues are called exceptions , and they
come in many varieties. Some exceptions you'll handle yourself; others will bub-
ble up and be handled by the VM or your application server. (Of course, its idea of
handling an exception may be to shut down the application.)
Once you've encountered a runtime error, you can use IDEA 's debugger to
investigate its cause. If you have access to a stack trace or log message for the
exception, it may help narrow down the source of the problem, possibly even giv-
ing the exact line number. But as we said, exceptions are inherently unexpected—
so the question is, what happened that wasn't expected? By executing the pro-
gram in the debugger, you can follow its progress, test your assumptions, and
observe the program data in a state of suspended animation. All of these features
should help you determine what went wrong.
Logic errors
Logic bugs are trickier beasts because they may not be readily apparent. With a
logic error, your application seems to work fine, but the outcome isn't as
expected. For example, if your new sorting method generates lists of data that
aren't properly sorted, you probably have a logic error.
 
 
 
 
 
Search WWH ::




Custom Search