南京家教,少儿频道音乐快递,588
实现了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有以下特点:
没有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) --------------
结论
有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) ------------------------------------------- ---------------------------------------------------------------------
结论
测试代码地址:https://github.com/kongxiangxin/pine/tree/master/auto-closeable
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论