public class ExceptionTest3 {
  public static void main(String[] args) {
    
    int[] a = {5, -1, 0};
    int b = 0;
    for(int i =0; i <= 3; i++) 
      try {
        b = middleMethod(a,i);
      } catch(ZeroDivideException e) {
        System.out.println(e.toString() + e.getIndex() + " is zero.");
        e.printStackTrace();
      } catch(ArrayIndexOutOfBoundsException e) {
        System.out.println("Array index out of bounds.");
      } catch(NegativeDivisorException e) {
         System.out.println(e.toString() + e.getDivisor());
        e.printStackTrace();
      } finally {
        System.out.println("Value of i is " + i);
      }
  }

  public static int middleMethod(int[] a, int i) throws ZeroDivideException,
    NegativeDivisorException {
    return lowMethod(a, i);
  }

  public static int lowMethod(int[] a, int i)  throws ZeroDivideException,
    NegativeDivisorException {
    try {
      if (a[i] < 0) throw new NegativeDivisorException(a[i]);
      return 15/a[i];
    }  catch(ArithmeticException e) {
      throw new ZeroDivideException(i);
    }  
  }
}
