Java Reference
In-Depth Information
Chapter 19
Handling Errors
Program Errors
Errors seem to be in the nature of computer systems. The logical complex-
ity of programs, as well as the mechanical diversity of the hardware, ad-
vises that we consider program errors as likely events. Ignoring the
possibility of errors leads to a “hope for the best” attitude in programming
that is both immature and dangerous. In this chapter we look at Java's ex-
tensive and powerful support for handling program errors.
Error Types
Program errors can originate in hardware, in software, or in algorithmic or
logical flaws. The possible solutions and the ideal error handling tech-
niques differ in each case.
Hardware and software errors
A program error can be hardware-related. For example, an application at-
tempts to open a file that does not exist, send characters to a printer that is
turned off, or communicate with a serial port that does not respond. Other
error conditions are software-related. For example, code attempts to ac-
cess an element that is beyond the bounds of the array or attempts to store a
value that exceeds the capacity of a data format.
Hardware-related errors are usually detected and reported by the sys-
tem. Software-related errors, on the other hand, must be detected by
code. Other errors can be detected either by software, by hardware, or by
both. For example, an application may inspect the divisor operand to
make sure that a division by zero is not attempted. However, if a division
Search WWH ::




Custom Search