Java Reference
In-Depth Information
1.3 Program Errors
In 1949, Maurice Wilkes, an early pioneer of computing, expressed a sentiment that
still rings true today:
As soon as we started programming, we found out to our surprise that it wasn't
as easy to get programs right as we had thought. Debugging had to be discov-
ered. I can remember the exact instant when I realized that a large part of
my life from then on was going to be spent in finding mistakes in my own
programs.
You also will have to face this reality as you learn to program. You're going to
make mistakes, just like every other programmer in history, and you're going to need
strategies for eliminating those mistakes. Fortunately, the computer itself can help
you with some of the work.
There are three kinds of errors that you'll encounter as you write programs:
Syntax errors occur when you misuse Java. They are the programming equivalent
of bad grammar and are caught by the Java compiler.
Logic errors occur when you write code that doesn't perform the task it is
intended to perform.
Runtime errors are logic errors that are so severe that Java stops your program
from executing.
Syntax Errors
Human beings tend to be fairly forgiving about minor mistakes in speech. For exam-
ple, we might find it to be odd phrasing, but we generally understand Master Yoda
when he says, “Unfortunate that you rushed to face him . . . that incomplete was
your training. Not ready for the burden were you.”
The Java compiler will be far less forgiving. The compiler reports syntax errors as
it attempts to translate your program from Java into bytecodes if your program breaks
any of Java's grammar rules. For example, if you misplace a single semicolon in your
program, you can send the compiler into a tailspin of confusion. The compiler
may report several error messages, depending on what it thinks is wrong with your
program.
A program that generates compilation errors cannot be executed. If you submit
your program to the compiler and the compiler reports errors, you must fix the errors
and resubmit the program. You will not be able to proceed until your program is free
of compilation errors.
Some development environments, such as Eclipse, help you along the way by
underlining syntax errors as you write your program. This makes it easy to spot
exactly where errors occur.
 
Search WWH ::




Custom Search