try-with-resources 实现自动关闭资源

try catch finally

对所有java开发人员而言,在finally代码块中关闭资源是再熟悉不过的处理方式了。异常处理的一部分工作,就是确保代码块中的各类资源被关闭和有效释放。一旦try-catch语句块中的代码抛出了异常,之前的工作流程就会发生改变。

try {
	//code statements(正常代码语句)
	//exception thrown here(抛出异常的语句)
	//lines not reached if exception thrown(异常抛出后无法执行的代码部分)
} catch (Exception e) {
	//lines reached only when exception is thrown(抛出异常后才会执行的代码部分)
} finally {
	//always executed irrespective of an exception thrown or not
	//(无论是否抛出异常都会执行的代码部分)
}

显然,上述代码中,关闭并释放各类资源的最佳位置,就是finally部分。这里提到的资源,就是像数据库连接、文件连接等等这样的具体实例或对象。

finally的执行顺序

1、finally不是必要条件
也就是说try-catch-finally中,可以只有try-catch,也可以只有try-finally。

2、假设基于try-catch-finally:
第一:代码没有异常
执行顺序:try执行完整->catch不执行->finally执行

第二:代码有异常且catch进行捕获**
执行顺序:try执行部分->跳转catch捕获处理->finally执行

第三:代码有异常且catch不捕获:这种情况没有catch**
执行顺序:try执行部分->finally执行
从上面的执行顺序可以看出,finally语句不管在哪种情况是一定会执行的。基于这个认识,现在我们再来分析。

题外话: 当finally有return时,会直接返回。不会再去返回try或者catch中的返回值,而finally没有return时,try和catch 的return语句并不会马上执行,而是执行完finally代码块之后再返回try和catch里面的值。

try-finally的缺点

同时打开了多个资源,那么将会出现噩梦般的场景:
程序员不得不显式地关闭某个资源,并且还要在finally中反复使用try-catch语句来处理这条关闭语句,这是java程序中典型的冗余代码。

...
InputStream is = null;
try {
	is = new FileInputStream("test");
	is.read();
	...
} catch (Exception e) {
	...
} finally {
	try {
		is.close();
	} catch (IOException e) {
		e.printStackTrace();
		...
	}
}

try-with-resources

try-with-resources是jdk1.7引入的语法糖,使得关闭资源操作无需层层嵌套在finally。实现自动关闭资源

要使用try-with-resource的资源,必须先实现AutoCloseable接口,其中包含了单个返回void的close方法,Java类库与第三方类库中的许多类和接口,现在都实现或扩展了AutoCloseable接口,因此我们现在不必实现了。

编译器会自动帮我们补全close()

资源的初始化工作是在try()中完成的。try后面引入了一对小括号(),用于在其中完成资源的初始化,示例如下:

try (InputStream is = new FileInputStream("test")) {
	is.read();
	...
} catch(Exception e) {
	...
} finally {
	//no need to add code to close InputStream, its close method will be internally called
	//(无需添加关闭InputStream输入流的代码,其close()方法会自行调用)
}


这样看上去,是不是感觉代码干净了许多,当程序运行完离开try语句块时,( )里的资源就会被自动关闭。

但是try-with-resources还有几个关键点要记住:
①、try()里面的类,必须实现了 AutoCloseable 接口 
②、在try()代码中 声明的 资源 被隐式 声明为fianl 
③、使用 分号 分隔,可以声明多个资源。

测试

创建狮子类Lion.java:

package com.javapapers.exceptionhandling;
 
public class Lion implements AutoCloseable {
	public Lion() {
		System.out.println("LION is OPEN in the wild.");
	};
 
	public void hunt() throws Exception {
		throw new Exception("DeerNotFound says Lion!");
	}
 
	public void close() throws Exception {
		System.out.println("LION is CLOSED in the cage.");
		throw new Exception("Unable to close the cage!");
	}
}

创建老虎类Tiger.java:

package com.javapapers.exceptionhandling;
 
public class Tiger implements AutoCloseable {
	public Tiger() {
		System.out.println("TIGER is OPEN in the wild.");
	};
 
	public void hunt() throws Exception {
		throw new Exception("DeerNotFound says Tiger!");
	}
 
	public void close() throws Exception {
		System.out.println("TIGER is CLOSED in the cage.");
	}
}

创建测试类TryWithResourcesExample.java:

package com.javapapers.exceptionhandling;
 
public class TryWithResourcesExample {
 
	public static void main(String[] args) {
		try (Lion lion = new Lion(); Tiger tiger = new Tiger()) {
 
			lion.hunt();
			tiger.hunt();
 
		} catch (Exception e) {
			System.out.println(e);
		} finally {
			System.out.println("Finally.");
		}
	}
}

Output for above example:(运行结果)

LION is OPEN in the wild.
TIGER is OPEN in the wild.
TIGER is CLOSED in the cage.
LION is CLOSED in the cage.
java.lang.Exception: DeerNotFound says Lion!
Finally.
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值