Try statements can be implicitly nested via calls to methods : try catch « Statement Control « Java Tutorial






class MethNestTry {
  static void nesttry(int a) {
    try {
      if (a == 1)
        a = a / (a - a);
      if (a == 2) {
        int c[] = { 1 };
        c[42] = 99; // generate an out-of-bounds exception
      }
    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Array index out-of-bounds: " + e);
    }
  }

  public static void main(String args[]) {
    try {
      int a = args.length;

      int b = 42 / a;
      System.out.println("a = " + a);

      nesttry(a);
    } catch (ArithmeticException e) {
      System.out.println("Divide by 0: " + e);
    }
  }
}








4.10.try catch
4.10.1.catch divide-by-zero error
4.10.2.Handle an exception and move on.
4.10.3.Demonstrate multiple catch statements.
4.10.4.Catch different Exception types
4.10.5.An example of nested try statements.
4.10.6.Try statements can be implicitly nested via calls to methods