Java Reference
In-Depth Information
error. You, the programmer, must flush out this type of error. Run the program, and
carefully look at its output.
A logic error causes a program to take an action that the programmer did not intend.
You must test your programs to find logic errors.
During program development, errors are unavoidable. Once a program is longer than a
few lines, it requires superhuman concentration to enter it correctly without slipping
up once. You will find yourself omitting semicolons or quotes more often than you
would like, but the compiler will track down these problems for you.
Logic errors are more troublesome. The compiler will not find themȌin fact, the
compiler will cheerfully translate any program as long as its syntax is correctȌbu t the
resulting program will do something wrong. It is the responsibility of the program
author to test the program and find any logic errors. Testing programs is an important
topic that you will encounter many times in this topic. Another important aspect of
good craftsmanship is defensive programming: structuring programs and development
processes in such a way that an error in one part of a program does not trigger a
disastrous response.
23
24
The error examples that you saw so far were not difficult to diagnose or fix, but as you
learn more sophisticated programming techniques, there will also be much more room
for error. It is an uncomfortable fact that locating all errors in a program is very
difficult. Even if you can observe that a program exhibits faulty behavior, it may not at
all be obvious what part of the program caused it and how you can fix it. Special
software tools (so-called debuggers) let you trace through a program to find bugsȌ
that is, logic errors. In Chapter 6 you will learn how to use a debugger effectively.
Note that these errors are different from the types of errors that you are likely to make
in calculations. If you total up a column of numbers, you may miss a minus sign or
accidentally drop a carry, perhaps because you are bored or tired. Computers do not
make these kinds of errors.
This topic uses a three-part error management strategy. First, you will learn about
common errors and how to avoid them. Then you will learn defensive programming
strategies to minimize the likelihood and impact of errors. Finally, you will learn
debugging strategies to flush out those errors that remain.
 
Search WWH ::




Custom Search