KayV
KayV

Reputation: 13845

AutoCloseable close() method exception not getting suppressed

I am working with the try-with-resources in a simple application in Java 8. I am implmenting the AutoCloseable interface and over riding the close() method. In one of my class which implements AutoCloseable, i am throwing an exception in the close() method, which would be working as the Suppressed Exception. And in my main method i am catching the exception. But the exception from the close method is not getting suppressed. That exception is being getting caught in the catch block as a general exception got caught.

Here is my code:

public class TryWithResourcesExample {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try (Lion lion = new Lion(); Tiger tiger = new Tiger()) {

            lion.hunt();
            tiger.hunt();

        } catch (Exception e) {
            System.out.println("Got Simple Exception = "+e);
            for(Throwable t: e.getSuppressed())
            {
                System.out.println("Got Suppressed Exception = "+t);
                t.printStackTrace();
            }
        } finally {
            System.out.println("Finally.");
        }
    }

}

class Tiger implements AutoCloseable {
    public Tiger() {
        System.out.println("TIGER is OPEN in the wild.");
    };

    public void hunt() throws Exception {
        //throw new Exception("DeerNotFound says Tiger!");
    }

    public void close() throws Exception {
        System.out.println("Tiger is CLOSED in the cage.");
    }
}

class Lion implements AutoCloseable {

    public Lion() {
        System.out.println("LION is OPEN in the wild.");
    }

    public void hunt() throws Exception {
        //throw new Exception("DeerNotFound says Lion!");
    }

    public void close() throws Exception {
        System.out.println("LION is CLOSED in the cage.");
        throw new Exception("Lion Unable to close the cage!");
    }
}

And here is my output:

LION is OPEN in the wild.
TIGER is OPEN in the wild.
Tiger is CLOSED in the cage.
LION is CLOSED in the cage.
Got Simple Exception = java.lang.Exception: Lion Unable to close the cage!
Finally.

Why is the exeception here in close method not getting suppressed?

Upvotes: 0

Views: 1724

Answers (1)

Flown
Flown

Reputation: 11740

I think you have to read the article about try-with-resource again.

Exceptions are only suppressed if there are more than one. Also an order is specified when and which exceptions are suppressed like in this example:

public class Test {
  public static void main(String... args) {
    try (ExceptionResource test = new ExceptionResource()) {
      test.work();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

class ExceptionResource implements AutoCloseable {

  public void work() throws Exception {
    throw new Exception("Thrown in 'run'");
  }

  @Override
  public void close() throws Exception {
    throw new Exception("Thrown in 'close'");
  }
}

Outcome:

java.lang.Exception: Thrown in 'run'
at test.ExceptionResource.work(Test.java:16)
at test.Test.main(Test.java:6)
Suppressed: java.lang.Exception: Thrown in 'close'
    at test.ExceptionResource.close(Test.java:21)
    at test.Test.main(Test.java:7)

Upvotes: 4

Related Questions