Possible Errors in Your Java Code
Ridding the editor of jagged underlines is cause for celebration. Eclipse likes the look of your Java code, so from that point on, it’s smooth sailing. Right?
Well, it ain’t necessarily so. In addition to some conspicuous compile-time errors, your code can have other, less obvious errors.
Imagine someone telling you to “go to the intersection, and then rurn tight.” You notice immediately that the speaker made a mistake, and you respond with a polite “Huh?” The nonsensical rurn tight phrase is like a compile-time error. Your “Huh?” is like the jagged underlines in Eclipse’s editor. As a listening human being, you may be able to guess what rurn tight means, but Eclipse’s editor never dares to fix your code’s mistakes.
In addition to compile-time errors, some other kinds of gremlins can hide inside a Java program:
- Unchecked runtime exceptions: You have no compile-time errors, but when you run your program, the run ends prematurely. Somewhere in the middle of the run, your instructions tell Java to do something that can’t be done.
This is an example of an unchecked runtime exception — the equivalent of someone telling you to turn right at the intersection when the only thing to the right is a big brick wall. Eclipse’s editor doesn’t warn you about an unchecked runtime exception because, until you run the program, the computer can’t predict that the exception will occur.
- Logic errors: You see no error markers in Eclipse’s editor, and when you run your code, the program runs to completion. But the answer isn’t correct. Let’s say that instead of $552.20, the output is $552,200,000.00. The program wrongly tells you to pay thousands of times what your house is worth and tells you to pay this amount each month! It’s the equivalent of being told to turn right instead of turning left. You can drive in the wrong direction for a very long time.
Logic errors are the most challenging errors to find and to fix. And worst of all, logic errors often go unnoticed. In March 1985, a homeowner got a monthly home heating bill for $1,328,932.21. Clearly, some computer had printed the incorrect amount. When he called the gas company to complain about it, the telephone service representative said, “Don’t be upset. Pay only half that amount.”
- Compile-time warnings: A warning isn’t as severe as an error message. So, when Eclipse notices something suspicious in your program, the editor displays a jagged yellow underline, a tiny yellow icon containing an exclamation point, and a few other not-so-intrusive clues.
Imagine being told to “turn when you reach the intersection.” The direction may be just fine. But if you’re suspicious, you ask, “Which way should I turn? Left or right?”
When you’re sure that you know what you’re doing, you can ignore warnings and worry about them at some later time. But a warning can be an indicator that something more serious is wrong with your code. Sweeping recommendation: Pay attention to warnings. But, if you can’t figure out why you’re getting a particular warning, don’t let the warning prevent you from moving forward.