11
C H A P T E R
Exceptions
If anything can go wrong, it will.
Finagle's Law
(often incorrectly attributed to Murphy, whose law is rather
different which only goes to show that Finagle was right)
W
HEN a Java program violates the semantic constraints of the Java language, a
Java Virtual Machine signals this error to the program as an
exception
. An exam
ple of such a violation is an attempt to index outside the bounds of an array. Some
programming languages and their implementations react to such errors by
peremptorily terminating the program; other programming languages allow an
implementation to react in an arbitrary or unpredictable way. Neither of these
approaches is compatible with the design goals of Java: to provide portability and
robustness. Instead, Java specifies that an exception will be thrown when semantic
constraints are violated and will cause a non local transfer of control from the
point where the exception occurred to a point that can be specified by the pro
grammer. An exception is said to be
thrown
from the point where it occurred and
is said to be
caught
at the point to which control is transferred.
Java programs can also throw exceptions explicitly, using
throw
statements
( 14.16). This provides an alternative to the old fashioned style of handling error
conditions by returning funny values, such as the integer value
1
where a nega
tive value would not normally be expected. Experience shows that too often such
funny values are ignored or not checked for by callers, leading to programs that
are not robust, exhibit undesirable behavior, or both.
Every exception is represented by an instance of the class
Throwable
or one
of its subclasses; such an object can be used to carry information from the point at
which an exception occurs to the handler that catches it. Handlers are established
by
catch
clauses of
try
statements ( 14.18). During the process of throwing an
exception, a Java Virtual Machine abruptly completes, one by one, any expres
sions, statements, method and constructor invocations, static initializers, and field
initialization expressions that have begun but not completed execution in the cur
201
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved