logoalt Hacker News

cogman10yesterday at 3:13 PM1 replyview on HN

Java's is

    try (var foo = new Foo()) {
    }
    // foo.close() is called here.
I like the Java method for things like files because if the there's an exception during the close of a file, the regular `IOException` block handles that error the same as it handles a read or write error.

Replies

mort96yesterday at 3:35 PM

What do you do if you wanna return the file (or an object containing the file) in the happy path but close it in the error path?

show 1 reply