10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

BLOCKS AND STATEMENTS Execution of try–catch–finally 14.20.2<br />

◆ If the finally block completes normally, then the try statement completes<br />

abruptly for reason R.<br />

◆ If the finally block completes abruptly for reason S, then the try statement<br />

completes abruptly for reason S (and reason R is discarded).<br />

<strong>The</strong> example:<br />

class BlewIt extends Exception {<br />

BlewIt() { }<br />

BlewIt(String s) { super(s); }<br />

}<br />

class Test {<br />

static void blowUp() throws BlewIt {<br />

throw new NullPointerException();<br />

}<br />

public static void main(String[] args) {<br />

try {<br />

blowUp();<br />

} catch (BlewIt b) {<br />

System.out.println("BlewIt");<br />

} finally {<br />

System.out.println("Uncaught Exception");<br />

}<br />

}<br />

}<br />

produces the output:<br />

Uncaught Exception<br />

java.lang.NullPointerException<br />

at Test.blowUp(Test.java:7)<br />

at Test.main(Test.java:11)<br />

<strong>The</strong> NullPointerException (which is a kind of RuntimeException) that is<br />

thrown by method blowUp is not caught by the try statement in main, because a<br />

NullPointerException is not assignable to a variable of type BlewIt. This<br />

causes the finally clause to execute, after which the thread executing main,<br />

which is the only thread of the test program, terminates because of an uncaught<br />

exception, which typically results in printing the exception name and a simple<br />

backtrace. However, a backtrace is not required by this specification.<br />

DRAFT<br />

401

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!