5.7. Exception Handling: try, catch, and finally

The mechanism for handling execeptions is embedded in the try-catch-finally construct, which has the following general form:

try {                                       // try block
    <statements>
} catch (<exception type1> <parameter1>) {  // catch block
    <statements>
}
...
  catch (<exception typen> <parametern>) {  // catch block
    <statements>
} finally {                                 // finally block
    <statements>
}

Exceptions thrown during execution of the try block can be caught and handled in a catch block. A finally block is guaranteed to be executed, regardless of the cause of exit from the try block, or whether any catch block was executed. Figure 5.9 shows three typical scenarios of control flow through the try-catch-finally construct.

Figure 5.9. try-catch-finally ...

Get Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.