jdk try with resource字节码

 try with resource可以查看编译后的字节码,会分别在程序结束末尾调用close,和抛异常后调用close

package test_httpserver;

import java.io.Closeable;
import java.io.IOException;

public class TestClose implements Closeable{

    @Override
    public void close() throws IOException {
        System.out.println("close");
    }

}
 

 

package test_httpserver;

public class TestTry {
    
    public static void main(String args[]) {
        try (TestClose testClose = new TestClose()){
            
        } catch (Exception e) {
            // TODO: handle exception
        }
        System.out.println("end");
    }
}
 

 

 

jdk try with resource字节码