Exception Example

Given the class ExceptionExample below, answer the following questions:

  1. What is the output if the program executes without any exceptions occurring?
  2. What is the output if NullPointerException occurs at line D?
  3. What is the output if NumberFormatException occurs at line F?
  4. What is the output if ArithmeticException occurs at line F?
  5. What is the output if ArithmeticException occurs at line C?
  6. What is the output if NumberFormatException occurs at line E?
  7. At what line(s) would there have to be an ArrayIndexOutOfBoundsException to cause the message "Error 1" to be printed (ignore other things that might be printed as well)?

public class ExceptionExample {
    public static void main(String [] args) {
        methodX();
	try {
	    methodY();
	} catch (ArrayIndexOutOfBoundsException aiobe) {
	    System.out.println("Error 1");
	} catch (ArithmeticException ae) {
	    System.out.println("Error 2");
	} finally {
	    System.out.println("Done");
	}
    }
    public static void methodX() {
        // Line A
	try {
	    // Line B
	} catch (NullPointerException npe) {
            System.out.println(npe.getMessage());
	}
    }
    public static void methodY() {
        // Line C
	try {
	    // Line D
	    methodZ();
	} catch (NullPointerException npe) {
            System.out.println("Error 3");
	} catch (ArrayIndexOutOfBoundsException aiobe) {
            System.out.println("Error 4");
	}
	System.out.println("Done methodY");
    }
    public static void methodZ() {
        // Line E
	try {
	    // Line F
	} catch (ArrayIndexOutOfBoundsException aiobe) {
            System.out.println(aiobe.getMessage());
	} catch (NumberFormatException nfe) {
            System.out.println("Error 5");
            return;
	} finally  {
            System.out.println("Done methodZ");
	}
    }
}