Exceptions Practice



Answers

What is the output of the following programs?

1)

public class Main {

  public static void main( String[] args ) {
    try { 
      a();
    } catch (NullPointerException npe) {
      System.out.println("main() catches npe");   
      System.out.println("crashing the program");
      throw npe;
    }
  }

  public static void a() {
    try {
      b();
    } catch (NullPointerException npe) {
      System.out.println("a() catches npe");
      System.out.println("a() throws npe to main()");
      throw npe;
    }
  }

  public static void b() {
    try {
      c();
    } catch (NullPointerException npe) {
      System.out.println("b() catches npe");
      System.out.println("b() throws npe to a()");
      throw npe;
    }
  }

  public static void c() {
    try {
      String s = null;
      int length = s.length();
    } catch (NullPointerException npe) {
      System.out.println("c() catches npe");
      System.out.println("c() throws npe to b()");
      throw npe;
    }
  }

}




2)

public class Main {

  public static void main( String[] args ) throws Exception {
    try {
      a();
    } catch (NullPointerException npe) {
      System.out.println("main() catches npe");
      System.out.println("crashing program");
      throw npe;
    } catch (ArrayIndexOutOfBoundsException aioobe) {
      System.out.println("main() catches aioobe");
    }
  }

  public static void a() throws Exception {
    try {
      b();
    } catch (Exception e) {
      System.out.println("a() catches e");
      System.out.println("a() throws e");
      throw e;
    }
  }  
   
  public static void b() throws RuntimeException {
    try {
      c();
    } catch (RuntimeException re) {
      System.out.println("b() catches re");
      System.out.println("b() throws re");
      throw re; 
    }
  }
 
  public static void c() {
    try {
      int[] x = new int[10];
      int[] y = copy(x);
      int z = y[7];
    } catch (NullPointerException npe) {
      System.out.println("c() catches npe");
      System.out.println("c() throws re");
      throw npe;
    } catch (ArrayIndexOutOfBoundsException aioobe) {
      System.out.println("c() catches aioobe");
      System.out.println("c() throws aioobe");
      throw aioobe;
    }
  }

  public static int[] copy(int[] source) {
    int[] copy = new int[source.length];
    for (int index = 0; index <= source.length; ++index) {
      copy[index] = source[index];
    }
    return null;
  }

}




3)

public class Main {

  public static void main( String[] args ) throws Exception {
    try {
      a();
    } catch (NullPointerException npe) {
      System.out.println("main() catches npe");
      System.out.println("crashing program");
      throw npe;
    } catch (ArrayIndexOutOfBoundsException aioobe) {
      System.out.println("main() catches aioobe");
    }
  }

  public static void a() throws Exception {
    try {
      b();
    } catch (Exception e) {
      System.out.println("a() catches e");
      System.out.println("a() throws e");
      throw e;
    }
  }

  public static void b() throws RuntimeException {
    try {
      c();
    } catch (RuntimeException re) {
      System.out.println("b() catches re");
      System.out.println("b() throws re");
      throw re; 
    }
  }
 
  public static void c() {
    try {
      int[] x = new int[10];
      int[] y = copy(x);
      int z = y[7];
    } catch (NullPointerException npe) {
      System.out.println("c() catches npe");
      System.out.println("c() throws re");
      throw npe;
    } catch (ArrayIndexOutOfBoundsException aioobe) {
      System.out.println("c() catches aioobe");
      System.out.println("c() throws aioobe");
      throw aioobe;
    }
  }

  public static int[] copy(int[] source) {
    int[] copy = new int[source.length];
    for (int index = 0; index < source.length; ++index) {
      copy[index] = source[index];
    }
    return null;
  }

}

Will the following programs compile?

4)

public class Main {

  public static void main( String[] args ) {
    try {
      a();
    } catch (NullPointerException npe) {
      System.out.println("main() catches npe");
      System.out.println("crashing program");
      throw npe;
    } catch (ArrayIndexOutOfBoundsException aioobe) {
      System.out.println("main() catches aioobe");
    }
  }

  public static void a() throws Exception {
    try {
      b();
    } catch (Exception e) {
      System.out.println("a() catches e");
      System.out.println("a() throws e");
      throw e;
    }
  }  
   
  public static void b() throws RuntimeException {
    try {
      c();
    } catch (RuntimeException re) {
      System.out.println("b() catches re");
      System.out.println("b() throws re");
      throw re; 
    }
  }
 
  public static void c() {
    try {
      int[] x = new int[10];
      int[] y = copy(x);
      int z = y[7];
    } catch (NullPointerException npe) {
      System.out.println("c() catches npe");
      System.out.println("c() throws re");
      throw npe;
    } catch (ArrayIndexOutOfBoundsException aioobe) {
      System.out.println("c() catches aioobe");
      System.out.println("c() throws aioobe");
      throw aioobe;
    }
  }

  public static int[] copy(int[] source) {
    int[] copy = new int[source.length];
    for (int index = 0; index < source.length; ++index) {
      copy[index] = source[index];
    }
    return null;
  }

}




5)

public class Main {

  public static void main( String[] args ) {
    try {
      a();
    } catch (NullPointerException npe) {
      System.out.println("main() catches npe");
      System.out.println("crashing program");
      throw npe;
    } catch (ArrayIndexOutOfBoundsException aioobe) {
      System.out.println("main() catches aioobe");
    } catch (Exception e) {
      System.out.println("main() catches e");      
    }
  }

  public static void a() {
    try {
      b();
    } catch (Exception e) {
      System.out.println("a() catches e");
      System.out.println("a() throws e");
      throw e;
    }
  }  
   
  public static void b() throws RuntimeException {
    try {
      c();
    } catch (RuntimeException re) {
      System.out.println("b() catches re");
      System.out.println("b() throws re");
      throw re; 
    }
  }
 
  public static void c() {
    try {
      int[] x = new int[10];
      int[] y = copy(x);
      int z = y[7];
    } catch (NullPointerException npe) {
      System.out.println("c() catches npe");
      System.out.println("c() throws re");
      throw npe;
    } catch (ArrayIndexOutOfBoundsException aioobe) {
      System.out.println("c() catches aioobe");
      System.out.println("c() throws aioobe");
      throw aioobe;
    }
  }

  public static int[] copy(int[] source) {
    int[] copy = new int[source.length];
    for (int index = 0; index < source.length; ++index) {
      copy[index] = source[index];
    }
    return null;
  }

}




6)

public class Main {

  public static void main( String[] args ) {
    try {
      a();
    } catch (NullPointerException npe) {
      System.out.println("main() catches npe");
      System.out.println("crashing program");
      throw npe;
    } catch (ArrayIndexOutOfBoundsException aioobe) {
      System.out.println("main() catches aioobe");
    } catch (Exception e) {
      System.out.println("main() catches e");      
    }
  }

  public static void a() throws Exception {
    try {
      b();
    } catch (Exception e) {
      System.out.println("a() catches e");
      System.out.println("a() throws e");
      throw e;
    }
  }

  public static void b() {
    try {
      c();
    } catch (RuntimeException re) {
      System.out.println("b() catches re");
      System.out.println("b() throws re");
      throw re; 
    }
  }
 
  public static void c() {
    try {
      int[] x = new int[10];
      int[] y = copy(x);
      int z = y[7];
    } catch (NullPointerException npe) {
      System.out.println("c() catches npe");
      System.out.println("c() throws re");
      throw npe;
    } catch (ArrayIndexOutOfBoundsException aioobe) {
      System.out.println("c() catches aioobe");
      System.out.println("c() throws aioobe");
      throw aioobe;
    }
  }

  public static int[] copy(int[] source) {
    int[] copy = new int[source.length];
    for (int index = 0; index < source.length; ++index) {
      copy[index] = source[index];
    }
    return null;
  }

}