Question soudaine. Que se passe-t-il si vous écrivez une clause finally dans la syntaxe try-with-resources? Bien sûr, attendez-vous à ce que Closeable (AutoCloseable) #close spécifié dans la clause try soit appelé, et la clause finally sera également appelée.
Main.java
public class Main {
    public static void main(String[] args) throws Exception {
        try (Hoge hoge = new Hoge()) {
            System.out.println("try");
        } finally {
            System.out.println("finally");
        }
    }
    public static class Hoge implements AutoCloseable {
        @Override
        public void close() throws Exception {
            System.out.println("close");
        }
    }
}
résultat
try
close
finally
C'est exactement ce à quoi je m'attendais. (C'est vrai) Une fois décompilé, cela ressemble à ceci.
Main.java
public class Main2 {
    public static void main(String[] args) throws Throwable {
        try {
            Throwable arg0 = null;
            Object arg1 = null;
            try {
                Main.Hoge hoge = new Main.Hoge();
                try {
                    System.out.println("try");
                } finally {
                    if (hoge != null) {
                        hoge.close();
                    }
                }
            } catch (Throwable arg14) {
                if (arg0 == null) {
                    arg0 = arg14;
                } else if (arg0 != arg14) {
                    arg0.addSuppressed(arg14);
                }
                throw arg0;
            }
        } finally {
            System.out.println("finally");
        }
    }
    public static class Hoge implements AutoCloseable {
        public void close() throws Exception {
            System.out.println("close");
        }
    }
}