'try(A a = new A())' VS 'try finally'
实现了AutoCloseable接口的类,可以在try的时候直接实例化对象。try代码块完成之后,自动调用close方法,相当于在finally里主动调用。但是出现异常后的流程和try finally有什么不同呢? 下面写代码测试一下。
首先定义一个类Cat,实现AutoCloseable接口
class Cat implements AutoCloseable{
void sayHello() throws Exception {
Utils.println("calling sayHello(), I will throw an exception");
throw new Exception("Exception in sayHello() ");
}
@Override
public void close() throws Exception {
Utils.println("I'm closing, I will throw an exception");
throw new Exception("Exception in close()");
}
}
我们的这个Cat有以下特点:
- sayHello方法会抛出异常
- close方法也会抛出异常
test v1: 'try(Cat cat = new Cat())' VS 'try finally'
没有catch(不要被外层的catch迷惑,那只是为了打印异常)
static void testV1(){
Utils.println("----try(Cat cat = new Cat())-----");
try{
try(Cat cat = new Cat()){
cat.sayHello();
}
}catch (Exception e){
Utils.println("cache error in main (" + e + "), let's see its stack trace");
Utils.printStackTrace(e);
}
Utils.println("--------------");
Utils.println("----try finally-----");
try{
Cat cat = null;
try{
cat = new Cat();
cat.sayHello();
}finally {
if(cat != null){
cat.close();
}
}
}catch (Exception e){
Utils.println("cache error in main (" + e + "), let's see its stack trace");
Utils.printStackTrace(e);
}
Utils.println("--------------");
}
结果输出:
----test v1----------------------------------------
----try(Cat cat = new Cat())-----
calling sayHello(), I will throw an exception
I'm closing, I will throw an exception
cache error in main (java.lang.Exception: Exception in sayHello() ), let's see its stack trace
java.lang.Exception: Exception in sayHello()
at Cat.sayHello(Cat.java:4)
at Test.testV1(Test.java:16)
at Test.main(Test.java:4)
Suppressed: java.lang.Exception: Exception in close()
at Cat.close(Cat.java:10)
at Test.testV1(Test.java:17)
... 1 more
--------------
----try finally-----
calling sayHello(), I will throw an exception
I'm closing, I will throw an exception
cache error in main (java.lang.Exception: Exception in close()), let's see its stack trace
java.lang.Exception: Exception in close()
at Cat.close(Cat.java:10)
at Test.testV1(Test.java:33)
at Test.main(Test.java:4)
--------------
结论
- try(Cat cat = new Cat())
- try代码块完成之后会自动调用close
- close抛出的异常,被Suppressed了,外层捕获的只有sayHello的异常,但通过堆栈可以找到这个Suppressed的异常
- try finally
- 外层捕获的是在finally执行close时抛出的异常,sayHello的异常完全不见了。
test v2: 'try(Cat cat = new Cat()) catch{}' VS 'try catch finally'
有catch,并且catch里再抛出异常
static void testV2(){
Utils.println("----try(Cat cat = new Cat()) catch-----");
try{
try(Cat cat = new Cat()){
cat.sayHello();
} catch (Exception e) {
Utils.println("cached err (" + e.getMessage() + "), I will throw an exception again");
throw new Exception("Exception in catch", e);
}
}catch (Exception e){
Utils.println("cache error in main (" + e + "), let's see its stack trace");
Utils.printStackTrace(e);
}
Utils.println("-----------------------------------------");
Utils.println("----try catch finally--------------------");
try{
Cat cat = null;
try{
cat = new Cat();
cat.sayHello();
} catch (Exception e) {
Utils.println("cached err (" + e.getMessage() + "), I will throw an exception again");
throw new Exception("Exception in catch", e);
}finally {
if(cat != null){
cat.close();
}
}
}catch (Exception e){
Utils.println("cache error in main (" + e + "), let's see its stack trace");
Utils.printStackTrace(e);
}
Utils.println("-------------------------------------------");
}
结果输出
----test v2------
----try(Cat cat = new Cat()){} catch{}-----
calling sayHello(), I will throw an exception
I'm closing, I will throw an exception
cached err (Exception in sayHello() ), I will throw an exception again
cache error in main (java.lang.Exception: Exception in catch), let's see its stack trace
java.lang.Exception: Exception in catch
at Test.testV2(Test.java:50)
at Test.main(Test.java:8)
-----------------------------------------
----try catch finally--------------------
calling sayHello(), I will throw an exception
cached err (Exception in sayHello() ), I will throw an exception again
I'm closing, I will throw an exception
cache error in main (java.lang.Exception: Exception in close()), let's see its stack trace
java.lang.Exception: Exception in close()
at Cat.close(Cat.java:10)
at Test.testV2(Test.java:70)
at Test.main(Test.java:8)
-------------------------------------------
---------------------------------------------------------------------
结论
- try(Cat cat = new Cat()) catch
- catch之前就调用了close(符合try代码块完成之后会自动调用close这个结论)
- catch到的是sayHello的异常,close抛出的异常依然被Suppressed了
- catch中再次抛出的异常被外层捕获
- try catch finally
- 先走catch,再走finally,所以catch捕获的是sayHello的异常
- catch中再次抛出的异常不见了,外层捕获的是在finally执行close时抛出的异常。
测试代码地址:https://github.com/kongxiangxin/pine/tree/master/auto-closeable
随机推荐
- 基于OceanStor Dorado V3存储之数据保护 Hyper 特性
基于OceanStor Dorado V3存储之数据保护 Hyper 特性 1.1 快照 1.2 HyperCDP 1.3 HyperCopy 1.4 克隆(HyperClone) 1.5 ...
- visa
- IIS错误:在唯一密钥属性“fileExtension”设置为“.json”时,无法添加类型为“mimeMap”的重复集合项
在用visual studio 打开一个asp.net mvc 项目时,ctrl+f5运行,发现页面无法加载图片.js.json文件. 按F12查看错误,发现500错误.打开报错的js文件,提示: I ...
- WebService 创建、发布、调用
环境Win7+VS2017 启用IIS 查看iis是否启用 新建 ASP.NET Web 应用程序 项目,项目中添加Web 服务 在 asmx 文件中添加需要的方法 运行结果 发布 创建新的文件夹, ...
- webpack4 打包静态资源
demo 代码点此,开始之前,先做点准备工作. 准备工作 准备一个空文件夹,然后执行下列命令: npm init -y npm i -D webpack webpack-cli 然后创建一个 dist ...
- js 加密混淆工具
访问路径:https://www.sojson.com/javascriptobfuscator.html
- maven 学习---Maven依赖机制
在 Maven 依赖机制的帮助下自动下载所有必需的依赖库,并保持版本升级. 案例分析 让我们看一个案例研究,以了解它是如何工作的.假设你想使用 Log4j 作为项目的日志.这里你要做什么? 1.在传统 ...
- spring boot的异常处理
原文:https://blog.csdn.net/tianyaleixiaowu/article/details/70145251 全局异常处理是个比较重要的功能,一般在项目里都会用到. 我大概把一次 ...
- BeautyWe.js 一套专注于微信小程序的开发范式
摘要: 小程序框架... 作者:JerryC 原文:BeautyWe.js 一套专注于微信小程序的开发范式 Fundebug经授权转载,版权归原作者所有. 官网:beautywejs.com Repo ...
- 关于k8s集群证书1年过期后,使用kubadm重新生成证书及kubeconfig配置文件的变化
这个证书很重要,不用说. 但手工生成证书,确实工作量大且容易出错. 推荐的方式,是保留/etc/kubernetes/pki目录下的ca.crt,ca.key,sa.crt,sa.key. 这四个文件 ...