'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
随机推荐
- 由OSS AccessKey泄露引发的思考
什么是OSS? 对象存储服务(Object Storage Service,OSS)是一种海量.安全.低成本.高可靠的云存储服务,适合存放任意类型的文件.容量和处理能力弹性扩展,多种存储类型供选择,全 ...
- C#,WPF,DataGrid,Excel,导出
private void btnExport_Click(object sender, RoutedEventArgs e) { System.Diagnostics.Stopwatch sw = n ...
- 大话区块链【Blockchain】
最近这几天区块链又粉墨登场了,新闻媒体也一直在大量报道,宣称可能要在金融界掀起一番浪潮.甚至有人说很久之前中国就出现了区块链的产物——麻将.那么区块链到底是什么,麻将和区块链又有什么关系呢? 笔者这两 ...
- 海关单一窗口程序出现网络/MQ问题后自动修复处理
单一窗口切换了2年多了,由于RabbitMQ或者网络的不稳定,或者升级或者网络调整,等等诸多问题导致了海关单一窗口程序会不定期的出现文件及回执自动处理的作业停止的问题. 最近终于想明白了,把日志监控起 ...
- (windows)局域网内无法访问FTP服务器的解决方法
遇见的问题 server windows.client windwos 本来好好的ftp,突然就客户端无法访问了 服务器端可以自己访问ftp 客户端ping的通服务器分析过程 因为公司管理严格,自己无 ...
- word转txt
import org.apache.poi.hwpf.extractor.WordExtractor; import java.io.IOException; import java.io.Input ...
- Webpack相关原理浅析
基本打包机制 本质上,webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler).当 webpack 处理应用程序时,它会递归地构建一个依赖关系图(de ...
- day 36
目录 pymysql操作mysql 安装 连接 增 删 改 查 索引 为什么使用索引以及索引的作用 类比 索引的本质 索引的底层原理 索引的种类(重点) 主键索引 唯一索引 普通索引 索引的创建 主键 ...
- JMETER 计数器的表现
1.计数器在线程中表现(虚拟用户). 线程组使用4个线程即4个虚拟用户. 如果不勾选每用户独立跟踪计数器. 那么这个计数器将会累加4次. 计算结果为7. 勾选的情况. 这里我们可以看到 index 一 ...
- odoo10学习笔记九:Odoo10 API
转载请转载原文地址:https://www.cnblogs.com/ygj0930/p/11189315.html 一:纪录集API model中的数据是以集合的形式使用的,因此可以使用集合运算来操作 ...