Java OCA OCP Practice Question 2832

Question

What is the output of the following?

public class Main {
   static class MyResource implements AutoCloseable {
      public void close() {
         throw new RuntimeException("fail");
      }/*from   w w  w.  j  a  v  a  2  s .  c  o m*/
   }

   public static void main(String[] args) {
      try (MyResource walk1 = new MyResource(); MyResource walk2 = new MyResource();) {
         throw new RuntimeException("fail2");
      } catch (Exception e) {
         System.out.println(e.getMessage() + " " + e.getSuppressed().length);
      }
   }
}
  • A. fail2 0
  • B. fail2 1
  • C. fail2 2
  • D. show 0
  • E. fail 1
  • F. fail 2
  • G. The code does not compile.


C.

Note

The exception inside the try block becomes the primary exception since it is thrown first.

Then two suppressed exceptions are added to it when trying to close the AutoCloseable resources.




PreviousNext

Related