java异常

最新更新地址:http://www.yiyehu.tech/archives/2018/02/03/java-try-catch

try-with-resources , 1.7版本后

try-with-resources 是 JDK 7 中一个新的异常处理机制,它能够很容易地关闭在 try-catch 语句块中使用的资源。所谓的资源(resource)是指在程序完成后,必须关闭的对象。try-with-resources 语句确保了每个资源在语句结束时关闭。所有实现了 java.lang.AutoCloseable 接口(其中,它包括实现了 java.io.Closeable 的所有对象),可以使用作为资源。

try (Resource res = new Resource()) {
            res.doSome();
} catch(Exception ex) {
            ex.printStackTrace();
}

你可能感兴趣的:(java)