try {
// The guarded region: Dangerous activities // that might throw A, B, or C } catch(A a1) { // Handler for situation A } catch(B b1) { // Handler for situation B } catch(C c1) { // Handler for situation C } finally { // Activities that happen every time } |
class FourException extends Exception {}
public class AlwaysFinally { public static void main(String[] args) { print("Entering first try block"); try { print("Entering second try block"); try { throw new FourException(); } finally { print("finally in 2nd try block"); } } catch(FourException e) { System.out.println( "Caught FourException in 1st try block"); } finally { System.out.println("finally in 1st try block"); } } } |
Entering first try block
Entering second try block finally in 2nd try block Caught FourException in 1st try block finally in 1st try block |