What if I write a finally clause in the try-with-resources syntax?

Sudden question. What happens if you write a finally clause in the try-with-resources syntax? Of course, expect that Closeable (AutoCloseable) #close specified in the try clause will be called, and finally clause will also be called.

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");
        }
    }

}

result

try
close
finally

It's exactly what I expected. (That's right) When decompiled, it looks like this.

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");
        }
    }
}

Recommended Posts

What if I write a finally clause in the try-with-resources syntax?
I got stuck trying to write a where in clause in ActiveRecord
Androd: What to do about "The Realm is already in a write transaction in"
What I learned when building a server in Java
[Rilas] What I learned in implementing the pagination function.
What I stumbled upon in the ActiveModel :: Serializer test
Even if I write the setting of STRICT_QUOTE_ESCAPING in CATALINA_OPTS in tomcat8.5, it is not reflected.
What to do if you get a java.io.IOException in GlassFish
How to create a placeholder part to use in the IN clause
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
I was confused because there was a split in the Array
Do I need a test if I do DDD in a language with types?
What to do if you get a gcc error in Docker
I tried to illuminate the Christmas tree in a life game
I tried to write code like a type declaration in Ruby
About what I did when creating a .clj file in Clojure
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
I recently made a js app in the rumored Dart language
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What to do if Cloud9 is full in the Rails tutorial
[Note] What I learned in half a year from inexperienced (Java) (3)
What to do if you get a DISPLAY error in gym.render ()
What I did in the migration from Spring Boot 1.5 series to 2.0 series
What to do if you forget the root password in CentOS7
I think it's okay to write joins etc. in the scope
What to do if the image posted by refile disappears after setting a 404 error page in Rails
What to do if Operation not permitted is displayed when you execute a command in the terminal
I created a PDF in Java.
What is a snippet in programming?
What are the rules in JUnit?
I want to write JSP in Emacs more easily than the default.
What to do if you get a groovy warning in Thymeleaf Layout
Write a null case using the Optional type without using an if statement
What to do if the prefix c is not bound in JSP
The parameters I received in Rails were a bit different than I expected
catch (Exception e) or catch (IOException e) is not required in the try-with-resources syntax
Memo: [Java] If a file is in the monitored directory, process it.
I can't build if I set the build destination to a simulator with XCode12!
What I did when JSF couldn't display database information in the view
I thought about the best way to create a ValueObject in Ruby
[Swift / Beginner] Let's write the functions around communication in a separate file
What to do if you get a javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake in the IBM JDK