Are resources closed before or after the finally?
The resource gets closed before catch or finally blocks. See this 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.
To evaluate this is a sample code:
class ClosableDummy implements Closeable {
public void close() {
System.out.println("closing");
}
}
public class ClosableDemo {
public static void main(String[] args) {
try (ClosableDummy closableDummy = new ClosableDummy()) {
System.out.println("try exit");
throw new Exception();
} catch (Exception ex) {
System.out.println("catch");
} finally {
System.out.println("finally");
}
}
}
Output:
try exit
closing
catch
finally