ExceptionDemo2.java

Download ExceptionDemo2.java

 1: // This code shows what happens when a method calls another,
 2: // which calls a third, which throws an exception.  The middle
 3: // method has a try-block, but it doesn't catch the type of
 4: // exception thrown; it does have a finally clause however.
 5: // Experiment with this code by changing the type of exception
 6: // thrown, so it is caught by one of the handlers.
 7: //
 8: // Written 1/2014 by Wayne Pollock, Tampa Florida USA
 9: 
10: public class ExceptionDemo2 {
11:     public static void main ( String [] args ) {
12:         try {
13:             aMethod();
14:             if (false) throw new java.io.IOException();
15:         }
16:         catch ( java.io.IOException any ) {
17:             System.out.println( "In main exception handler" );
18:         }
19:     }
20: 
21:     public static void aMethod () {
22:         try {
23:             bMethod();
24:         }
25:         catch ( ArithmeticException ae ) {
26:             System.out.println( "In aMethod's exception handler" );
27:         }
28:         finally {
29:             System.out.println( "In aMethod's finally clause" );
30:         }
31:     }
32: 
33:     public static void bMethod () {
34:         throw new ArrayIndexOutOfBoundsException("Oops!");
35:     }
36: }