O'Reilly logo

Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition by Rolf W. Rasmussen, Khalid A. Mughal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required