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 />

completes abruptly for any reason, then the try statement completes<br />

abruptly for the same reason.<br />

◆ If the run-time type of V is not assignable to the parameter of any catch<br />

clause of the try statement, then the try statement completes abruptly<br />

because of a throw of the value V.<br />

• If execution of the try block completes abruptly for any other reason, then<br />

the try statement completes abruptly for the same reason.<br />

In the example:<br />

class BlewIt extends Exception {<br />

BlewIt() { }<br />

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

}<br />

class Test {<br />

static void blowUp() throws BlewIt { throw new BlewIt(); }<br />

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

try {<br />

blowUp();<br />

} catch (RuntimeException r) {<br />

System.out.println("RuntimeException:" + r);<br />

} catch (BlewIt b) {<br />

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

}<br />

}<br />

}<br />

the exception BlewIt is thrown by the method blowUp. <strong>The</strong> try–catch statement<br />

in the body of main has two catch clauses. <strong>The</strong> run-time type of the exception is<br />

BlewIt which is not assignable to a variable of type RuntimeException, but is<br />

assignable to a variable of type BlewIt, so the output of the example is:<br />

BlewIt<br />

DRAFT<br />

14.20.2 Execution of try–catch–finally<br />

After the great captains and engineers have accomplish’d their work,<br />

After the noble inventors—after the scientists, the chemist,<br />

the geologist, ethnologist,<br />

Finally shall come the Poet . . .<br />

A try statement with a finally block is executed by first executing the try<br />

block. <strong>The</strong>n there is a choice:<br />

399

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

Saved successfully!

Ooh no, something went wrong!