Exception Example Answers

Given the class ExceptionExample, answer the following questions:

  1. What is the output if the program executes without any exceptions occurring?
    Done methodZ
    Done methodY
    Done

  2. What is the output if NullPointerException occurs at line D?
    Error 3
    Done methodY
    Done

  3. What is the output if NumberFormatException occurs at line F?
    Error 5
    Done methodZ
    Done methodY
    Done

  4. What is the output if ArithmeticException occurs at line F?
    Done methodZ
    Error 2
    Done

  5. What is the output if ArithmeticException occurs at line C?
    Error 2
    Done

  6. What is the output if NumberFormatException occurs at line E?
    Done
    Program crashes, stack trace is printed

  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)?
    Only at Line C

Additional questions to consider:

  1. What is the output if ArrayIndexOutOfBoundsException occurs at line A?
  2. What is the output if NullPointerException occurs at line B?
  3. What is the output if ArrayIndexOutOfBoundsException occurs at line D?
  4. What is the output if NullPointerException occurs at line E?
  5. What is the output if ArrayIndexOutOfBoundsException occurs at line E?
  6. What is the output if ClassCastException occurs at line F?