Charles Spencer
Charles Spencer

Reputation: 647

Exception thrown during try-with-resources declaration

Say I have the following try-with-resources statement in Java:

try (MyResource myResource1 = new MyResource(); MyResource myResource2 = new MyResource()) {
    // do stuff...
}

If MyResource myResource2 = new MyResource() throws an exception, is it guaranteed that myResource1.close() will be called?

Upvotes: 9

Views: 4017

Answers (2)

M A
M A

Reputation: 72844

You can easily try it:

public static void main(String[] args) {
    try (Resource1 myResource1 = new Resource1(); Resource2 myResource2 = new Resource2()) {
        // do stuff...
    }
}


class Resource1 implements AutoCloseable {

    @Override
    public void close() {
        System.out.println("Closing Resource1...");
    }

}

class Resource2 implements AutoCloseable {

    public Resource2() {
        throw new RuntimeException("Bad resource!");
    }

    @Override
    public void close() {
        System.out.println("Closing Resource2...");
    }

}

Output:

Closing Resource1...

Exception in thread "main" java.lang.RuntimeException: Bad resource!

which shows the first resource is closed.

Upvotes: 4

Tunaki
Tunaki

Reputation: 137064

Yes, this is guaranteed. Quoting from JLS section 14.20.3:

Resources are initialized in left-to-right order. If a resource fails to initialize (that is, its initializer expression throws an exception), then all resources initialized so far by the try-with-resources statement are closed. If all resources initialize successfully, the try block executes as normal and then all non-null resources of the try-with-resources statement are closed.

In this case, if the second new MyResource() throws an exception, since myResource1 was successfully initialized, it will be closed.

Upvotes: 19

Related Questions