Java OCA OCP Practice Question 273

Question

Given:

public class Main {
   class MyClass implements Closeable {
      public void close() {
         throw new RuntimeException("a");
      }/*  w  w  w .j  av  a2 s .c o m*/
   }

   public static void main(String[] args) {
      new Main().run();
   }

   public void run() {
      try (MyClass l = new MyClass();) {
         throw new IOException();
      } catch (Exception e) {
         throw new RuntimeException("c");
      }
   }
}

Which exceptions will the code throw?

  • A. IOException with suppressed RuntimeException a
  • B. IOException with suppressed RuntimeException c
  • C. RuntimeException a with no suppressed exception
  • D. RuntimeException c with no suppressed exception
  • E. RuntimeException a with suppressed RuntimeException c
  • F. RuntimeException c with suppressed RuntimeException a
  • G. Compilation fails


D is correct.

Note

While the exception caught by the catch block matches choice A, it is ignored by the catch block.

The catch block just throws RuntimeException c without any suppressed exceptions.




PreviousNext

Related