java try with_Java---异常(Try-With-Resource 的用法)

Try-With-Resource 的用法

在 Java7 之前,如果我们的程序打开了一个IO 流,那么在发生异常或者运行结束时需要调用 close() 关闭 IO 流。那么需要像这样编写代码

public class MessyExceptions {

public static void main(String[] args) {

InputStream in = null;

try {

in = new FileInputStream(

new File("MessyExceptions.java"));

int contents = in.read();

// Process contents

} catch(IOException e) {

// Handle the error

} finally {

if(in != null) {

try {

in.close();

} catch(IOException e) {

// Handle the close() error

}

}

}

}

}

代码中 finally 语句块中还包含着 try 语句,使得代码看起来过于复杂。

而在 Java7 引入 try-with-resources 语法,可以简化上述代码

public class TryWithResources {

public static void main(String[] args) {

try(

InputStream in = new FileInputStream(

new File("TryWithResources.java"))

) {

int contents = in.read();

// Process contents

} catch(IOException e) {

// Handle the error

}

}

}

try-with-resources 语法

try(){

}catch(){

}

在 try-with-resources 定义子句中创建的对象(在括号内的对象)必须实现 java.lang.AutoCloseable 接口,这个接口有一个方法:close()。

当 try() 括号里面的 IO 出现异常或者运行结束,会自动调用 close() 关闭对象。

try-with-resources 中的 try 语句也可以不加 catch 和 finall 而独立存在。

try-with-resources 基本运行机制

创建自定义实现 AutoCloseable 接口的类

class Reporter implements AutoCloseable {

String name = getClass().getSimpleName();

Reporter() {

System.out.println("Creating " + name);

}

public void close() {

System.out.println("Closing " + name);

}

}

class First extends Reporter {}

class Second extends Reporter {}

public class AutoCloseableDetails {

public static void main(String[] args) {

try(

First f = new First();

Second s = new Second()

) {

}

}

}

输出

Creating First

Creating Second

Closing Second

Closing First

退出 try 块会调用两个对象的 close() 方法,并以与创建顺序相反的顺序关闭它们。顺序很重要,因为在此配置中,Second 对象可能依赖于 First 对象,因此如果 First 在第 Second 关闭时已经关闭。 Second 的 close() 方法可能会尝试访问 First 中不再可用的某些功能。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值