标签:detail china led exce str dna flush closeable 参考
java.lang.AutoCloseable
和java.io.Closeable
public interface AutoCloseable {
void close() throws Exception;
}
public interface Closeable extends AutoCloseable {
public void close() throws IOException;
}
java.lang.AutoCloseable
是JDK7
添加的新接口try-with-resource
提供支持java.io.Closeable
是JDK5
添加的接口。从JDK7开始,Closeable扩展了AutoCloseable。因此,在JDK7中,所有实现了Closeable接口的类也都实现了AutoCloseable接口。public class TryWithResource implements AutoCloseable {
@Override
public void close() throws Exception {
StackTraceElement stack[] = Thread.currentThread().getStackTrace();
for (StackTraceElement ste : stack) {
System.out
.println("called by " + ste.getClassName() + "." + ste.getMethodName() + " / " + ste.getFileName());
}
}
public static void main(String[] args) {
try (TryWithResource twr = new TryWithResource()) {
System.out.println("use end...");
throw new Exception("xxx");
} catch (Exception e) {
System.out.println("close error...");
}
}
}
标签:detail china led exce str dna flush closeable 参考
原文地址:https://www.cnblogs.com/huangwenjie/p/9292910.html