Try-with-resources: Must I throw or catch the close() method's exceptions?

JavaException Handling

Java Problem Overview


Please correct me if this is wrong: In Java 7's try-with-resources statement, any exceptions thrown by the resource's close() method must be either declared as thrown by my method or I must wrap the whole try in another try which catches any exceptions thrown by close().

If so, I have to wonder if I will make much use of it. I certainly don't want to throw the exceptions thrown by close(), the caller won't know what to do with that. And a try wrapping another try just to handle close() would not look very elegant, to me at least.

EDIT: I think I accidentally asked two questions, one of which was a duplicate.

Question 1. Do I have to declare that my method throws the exception from the close() method or wrap the try-with-resources in another try? (Not answered in proposed duplicate.)

Question 2. Is there a way to close the resource silently? (Clearly a duplicate, so I am taking that sentence out of the question. Hopefully this makes the question satisfactorily unique.)

Java Solutions


Solution 1 - Java

Quote from Java Language Specification ($14.20.3.2): > 14.20.3.2 Extended try-with-resources > A try-with-resources statement with at least one catch clause and/or a finally clause is called an extended try-with-resources statement. The meaning of an extended try-with-resources statement: > >    try ResourceSpecification
>        Block
    Catchesopt
    Finallyopt
> is given by the following translation to a basic try-with-resources statement (§14.20.3.1) nested inside a try-catch or try-finally or try-catch-finally statement:
>     try {
        try ResourceSpecification
            Block
    }
    Catchesopt
    Finallyopt
> The effect of the translation is to put the ResourceSpecification "inside" the try statement. This allows a catch clause of an extended try-with-resources statement to catch an exception due to the automatic initialization or closing of any resource.

So, basically, wrapper is already implemented

Solution 2 - Java

From the Java tutorial

> A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.

(emphasis mine)

So you can simply do

try (BufferedReader br =
               new BufferedReader(new FileReader(path))) {
    return br.readLine();
}
catch (IOException e) {
    // handle the exception that has been thrown by readLine() OR by close().
}

Solution 3 - Java

You do not need to wrap the try-with-resources in another try-catch block, you simply add a catch block:

class Foo implements AutoCloseable {
    public void close() throws Exception {
        throw new Exception();
    }
}

public class Try {
    public static void main(final String[] args) {
        try(Foo f = new Foo()) {
            System.out.println("No op!");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Solution 4 - Java

You should be able to simply add the appropriate catch (Exception e) { } clause. If you need to do special handling for a specific one, or you can simply catch Exception, if you need it much broader.

try (Statement stmt = con.createStatement()) {
    ResultSet rs = stmt.executeQuery(query);

    while (rs.next()) {
        String coffeeName = rs.getString("COF_NAME");
        int supplierID = rs.getInt("SUP_ID");
        float price = rs.getFloat("PRICE");

        System.out.println(coffeeName + ", " + supplierID + ", " + 
                           price + ", " + sales + ", " + total);
    }
} catch (Exception e) {
    System.out.println("Exception while trying to through the queries. ", e);
}

Since it's Java 7, you can actually put multiple exceptions per catch clause, or you can simply catch the outermost exception you want.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn FitzpatrickView Question on Stackoverflow
Solution 1 - Javadefault localeView Answer on Stackoverflow
Solution 2 - JavaJB NizetView Answer on Stackoverflow
Solution 3 - JavaPerceptionView Answer on Stackoverflow
Solution 4 - JavamjuarezView Answer on Stackoverflow