14.18.2
Execution of
try catch finally
BLOCKS AND STATEMENTS
If execution of the
try
block completes abruptly for any other reason
R
, then
the
finally
block is executed. Then there is a choice:
N
If the
finally
block completes normally, then the
try
statement completes
abruptly for reason
R
.
N
If the
finally
block completes abruptly for reason
S
, then the
try
state
ment completes abruptly for reason
S
(and reason
R
is discarded).
The example:
class BlewIt extends Exception {
BlewIt() { }
BlewIt(String s) { super(s); }
}
class Test {
static void blowUp() throws BlewIt {
throw new NullPointerException();
}
public static void main(String[] args) {
try {
blowUp();
} catch (BlewIt b) {
System.out.println("BlewIt");
} finally {
System.out.println("Uncaught Exception");
}
}
}
produces the output:
Uncaught Exception
java.lang.NullPointerException
at Test.blowUp(Test.java:7)
at Test.main(Test.java:11)
The
NullPointerException
(which is a kind of
RuntimeException
) that is
thrown by method
blowUp
is not caught by the
try
statement in
main
, because a
NullPointerException
is not assignable to a variable of type
BlewIt
. This
causes the
finally
clause to execute, after which the thread executing
main
,
which is the only thread of the test program, terminates because of an uncaught
exception ( 20.21.31), which results in printing the exception name and a simple
backtrace.
294
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