Sample Program - Exception Propagation


//  this program shows how exception propagates to the caller if it 
//  isn't caught in the method that raises it

public class ExcepUpTest {
   public static void main(String[] args) {
      int[] nums = new int[10];
      try {
         for (int i = 0 ; i < nums.length ; i++)
            nums[i] = 3 * i;
         sub(nums);
         for (int i = 0 ; i < nums.length ; i++)
            System.out.println(nums[i]);
      }
      catch(Exception e) {
         System.out.println("Exception occurred");
         System.out.println("Of type " + e);
      }
   }

//  sub will raise an exception on the array reference
 
   public static void sub(int[] nums) {
      System.out.println("Now in sub");
      nums[15] = 15;
      System.out.println("Now leaving sub");
   }
}

Output:

Now in sub
Exception occured
Of type java.lang.ArrayIndexOutOfBoundsException


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page