What's the use of finally block in try with resources(Java 7)?

Viewed 2182

The finally block is mainly used to prevent resource leaks which can be achieved in close() method of resource class. What's the use of finally block with try-with-resources statement, e.g:

class MultipleResources {

    class Lamb implements AutoCloseable {
        public void close() throws Exception {
            System.out.print("l");
        } 
    }

    class Goat implements AutoCloseable {
        public void close() throws Exception {
            System.out.print("g");
        } 
    }

    public static void main(String[] args) throws Exception {
        new MultipleResources().run();
    }

    public void run() throws Exception {
        try (Lamb l = new Lamb(); Goat g = new Goat();) {
            System.out.print("2");
        } finally {
            System.out.print("f");
        }
    }
}

Ref: K.Seirra, B. Bates OCPJP Book

2 Answers
Related