Java OCA OCP Practice Question 1791

Question

Which statement about the following program is true?

package mypkg;/*from  w w w  .  j  a  v  a2  s. c  om*/
class Exception1 extends Exception {}
public class Phone {
   static void m() throws RuntimeException {
      throw new ArrayIndexOutOfBoundsException("Call");
   }
   public static void main(String[] messages) {
     try {
        m();
     } catch (Exception1 e) {
        throw new RuntimeException("Voicemail");
     } finally {
        throw new RuntimeException("Text");
     }
  }
}
  • A. An exception is printed at runtime with Call in the message.
  • B. An exception is printed at runtime with Voicemail in the message.
  • C. An exception is printed at runtime with Text in the message.
  • D. The code does not compile.


D.

Note

The Exception1 is a checked exception since it extends Exception and does not inherit RuntimeException.

The first catch block fails to compile, since the compiler detects that it is not possible to throw this checked exception inside the try block, making Option D the correct answer.

If Exception1 was changed to extend the checked RuntimeException, then the code would compile.




PreviousNext

Related