java trywithresources_Java try-with-resources用法

在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()获得。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值