11.3
Handling of an Exception
EXCEPTIONS
Java language can result in runtime exceptions. The information available to a
Java compiler, and the level of analysis the compiler performs, are usually not suf
ficient to establish that such runtime exceptions cannot occur, even though this
may be obvious to the Java programmer. Requiring such exception classes to be
declared would simply be an irritation to Java programmers.
For example, certain code might implement a circular data structure that, by
construction, can never involve
null
references; the programmer can then be
certain that a
NullPointerException
cannot occur, but it would be difficult for a
compiler to prove it. The theorem proving technology that is needed to establish
such global properties of data structures is beyond the scope of this Java Language
Specification.
11.3 Handling of an Exception
When an exception is thrown, control is transferred from the code that caused the
exception to the nearest dynamically enclosing
catch
clause of a
try
statement
( 14.18) that handles the exception.
A statement or expression is
dynamically enclosed
by a
catch
clause if it
appears within the
try
block of the
try
statement of which the
catch
clause is a
part, or if the caller of the statement or expression is dynamically enclosed by the
catch
clause.
The
caller
of a statement or expression depends on where it occurs:
If within a method, then the caller is the method invocation expression
( 15.11) that was executed to cause the method to be invoked.
If within a constructor or the initializer for an instance variable, then the caller
is the class instance creation expression ( 15.8) or the method invocation of
newInstance
that was executed to cause an object to be created.
If within a static initializer or an initializer for a
static
variable, then the
caller is the expression that used the class or interface so as to cause it to be
initialized.
Whether a particular
catch
clause
handles
an exception is determined by
comparing the class of the object that was thrown to the declared type of the
parameter of the
catch
clause. The
catch
clause handles the exception if the type
of its parameter is the class of the exception or a superclass of the class of the
exception. Equivalently, a
catch
clause will catch any exception object that is an
instanceof
( 15.19.2) the declared parameter type.
The control transfer that occurs when an exception is thrown causes abrupt
completion of expressions ( 15.5) and statements ( 14.1) until a
catch
clause is
204
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