在Java 7之前,IO操作为了保证资源能被关闭,往往会在try代码块后加上finally代码块来处理资源的关闭。
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
if (br != null) br.close();
}
Java 7新增了try-with-resources语法来保证资源的关闭
try(BufferedReader br = new BufferedReader(new FileReader(path));) {
return br.readLine();
}
BufferedReader 会在代码块里处理完后会被关闭,不需要显式调用BufferedReader的close方法。
使用方法
被关闭的资源类需要实现AutoClosable接口,Closeable接口继承于AutoClosable,所以实现了Closable接口的BufferedReader可以在try-with-resources被自动关闭。
需要自动关闭的资源在try后面的括号里声明。允许声明多个被关闭的资源,关闭的顺序是与创建资源的顺序相反。
异常处理
在传统的try-catch-finally方式处理异常时,如果在finally代码块里有异常抛出,try代码块里抛出的异常会被抑制。
public void testTryWithFinally() {
try {
throw new RuntimeException("try exception");
} finally {
throw new RuntimeException("finally exception");
}
}
在这个示例里,虽然try代码块里先抛出了异常,但是外部调用只捕获到finally抛出的异常,try代码块抛出的异常被抑制。
try-with-resources里关闭资源时都允许抛出异常,但与传统的try-catch-finally处理异常不同,如果try执行的代码块里抛出异常,那么try-with-resources关闭资源抛出的异常将被抑制。
public void testTryWithResources() throws Exception {
try (ClosableResource resource = new ClosableResource()) {
throw new RuntimeException("try exception");
}
}
class ClosableResource implements AutoCloseable{
@Override
public void close() throws Exception {
throw new RuntimeException("close resource runtime exception");
}
}
在这个示例里,ClosableResource 被关闭时会抛出运行时异常,但是在try执行的代码块里也有异常抛出,外部调用捕获的将是try执行代码块里的异常。
关闭资源与catch-finally执行顺序
try-with-resources也可以添加catch和finally语句,但catch和finally里的代码在资源被关闭后才会被执行。
public void testTryWithResources2() throws IOException {
try (ClosableResource resource = new ClosableResource()) {
throw new RuntimeException("try exception");
} catch (Exception e) {
System.out.println("catch exception:" + e.getMessage());
System.out.println("suppressed exception:" + e.getSuppressed()[0].getMessage());
} finally {
System.out.println("finally exception");
}
}
class ClosableResource implements AutoCloseable{
@Override
public void close() throws Exception {
System.out.println("close resource");
throw new RuntimeException("close resource runtime exception");
}
}
输出的顺序是
close resource
catch exception:try exception
suppressed exception:close resource runtime exception
finally exception
输出顺序是关闭资源,catch捕获try里抛出的异常,最后才是执行finally代码。其中关闭资源抛出的异常可以使用Throwable.getSuppressed()获得。