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字节码

相关文章:

  • 2021-06-03
  • 2021-05-01
  • 2021-07-08
  • 2021-06-14
  • 2021-08-14
  • 2021-12-03
  • 2021-11-12
  • 2021-05-26
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-20
  • 2022-12-23
  • 2021-09-27
  • 2021-12-04
  • 2022-12-23
相关资源
相似解决方案