5

I have come across one doubt in "Try with Resources" topic .

Program code :

public class Suppressed_Exception_Eg03 { public static void main(String[] args) { try (Wolf obj = new Wolf(); Deer obj1 = new Deer();) { //Both close statements are executed . //Therefore , we see two closing stmts } catch(Exception e) { System.out.println(e.getMessage()); } } static class Wolf implements AutoCloseable { @Override public void close() throws Exception { System.out.println("Closing Wolf !"); throw new RuntimeException("In Wolf !"); } } static class Deer implements AutoCloseable { @Override public void close() throws Exception { System.out.println("Closing Deer !"); throw new RuntimeException("In Deer !"); } } 

Output :

Closing Deer ! Closing Wolf ! In Deer ! 

Doubt : We all are aware that close method for Deer class will be closed first and following will be of Wolf class . Accordingly , the exception thrown by Wolf class should suppress the exception thrown by Deer class . So , we should be catching Wolf class' exception in catch block . But here we can see in the output , Deer class' exception is caught and printed . Can some one explain why is that ?

6
  • 2
    Please format your code by indenting each block four spaces, by making sure each line on the same block or the same block level is indented the exact same amount as the others. Yes this is a picky request, but again, we're volunteers, and you want to put in the effort to make it easy for others to help you. Commented Aug 26, 2016 at 10:57
  • The runtime exception boots you out of the try block. If this were my code and problem, I'd check the bytecode. Commented Aug 26, 2016 at 11:00
  • 1
    Reading documenation is always so helpful: "Note that the close methods of resources are called in the opposite order of their creation." ... Commented Aug 26, 2016 at 11:02
  • .... and you still haven't formatted your code. Why? Commented Aug 26, 2016 at 11:36
  • I don't understand how to format my post . It looks totally fine to me . Commented Aug 27, 2016 at 5:11

1 Answer 1

6

The spec says:

Resources are closed in the reverse order from that in which they were initialized. A resource is closed only if it initialized to a non-null value. An exception from the closing of one resource does not prevent the closing of other resources. Such an exception is suppressed if an exception was thrown previously by an initializer, the try block, or the closing of a resource.

The first exception in your code (Deer) is not suppressed because there was no previous exceptions being thrown. Then, the next resource is closed (Wolf), but this time the exception from Wolf is suppressed.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi David , I did not know that "An exception from the closing of one resource does not prevent the closing of other resources. " and "an exception is suppressed if an exception was thrown previously by an initializer, the try block, or the closing of a resource". If that is the case then the output is justified . Thanks David !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.