23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

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.

try<br />

ma<strong>in</strong>_block_of_statements<br />

catch (exception_type 1 variable 1 )<br />

block_of_statements 1<br />

catch (exception_type 2 variable 2 )<br />

…<br />

block_of_statements 2<br />

f<strong>in</strong>ally<br />

block_of_statements n<br />

where there must be at least one catch part, but the f<strong>in</strong>ally part is optional.<br />

Each exception_type i is the type of some exception, <strong>and</strong> each variable i is a valid<br />

<strong>Java</strong> variable name.<br />

The <strong>Java</strong> run-time environment beg<strong>in</strong>s perform<strong>in</strong>g a try-catch block such as<br />

this by execut<strong>in</strong>g the block of statements, ma<strong>in</strong>_block_of_statements. If this<br />

execution generates no exceptions, then the flow of control cont<strong>in</strong>ues with the first<br />

statement after the last l<strong>in</strong>e of the entire try-catch block, unless it <strong>in</strong>cludes an<br />

optional f<strong>in</strong>ally part. The f<strong>in</strong>ally part, if it exists, is executed regardless of<br />

whether any exceptions are thrown or caught. Thus, <strong>in</strong> this case, if no exception is<br />

thrown, execution progresses through the try-catch block, jumps to the<br />

f<strong>in</strong>ally part, <strong>and</strong> then cont<strong>in</strong>ues with the first statement after the last l<strong>in</strong>e of the<br />

try-catch block.<br />

If, on the other h<strong>and</strong>, the block, ma<strong>in</strong>_block_of_statements, generates an<br />

exception, then execution <strong>in</strong> the try-catch block term<strong>in</strong>ates at that po<strong>in</strong>t <strong>and</strong><br />

execution jumps to the catch block whose exception_type most closely matches<br />

the exception thrown. The variable for this catch statement references the exception<br />

object itself, which can be used <strong>in</strong> the block of the match<strong>in</strong>g catch statement.<br />

Once execution of that catch block completes, control flow is passed to the<br />

optional f<strong>in</strong>ally block, if it exists, or immediately to the first statement after the<br />

last l<strong>in</strong>e of the entire try-catch block if there is no f<strong>in</strong>ally block. Otherwise,<br />

if there is no catch block match<strong>in</strong>g the exception thrown, then control is passed to<br />

the optional f<strong>in</strong>ally block, if it exists, <strong>and</strong> then the exception is thrown back to<br />

the call<strong>in</strong>g method.<br />

Consider the follow<strong>in</strong>g example code fragment:<br />

<strong>in</strong>t <strong>in</strong>dex = Integer.MAX_VALUE; // 2.14 Billion<br />

114

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

Saved successfully!

Ooh no, something went wrong!