之前碰到了这类问题,然后在CSDN上看到了这方面的讲解了,觉得获益匪浅。自己也去实践了一下,稍微分享一下心得。第一次发博,表述错误与不清楚的地方多多包涵。
基本测试
先上测试代码,logger功能可以看成print
@Testpublic void tryCatchFinallyTest() {Long startTimeMilSec; Long endTimeMilSec; startTimeMilSec = System.currentTimeMillis(); logger.info("start test, current time : {}", startTimeMilSec); String returnInfo = tryCatchFinallyTestFun().getInfo(); logger.info("return info : {}", returnInfo); endTimeMilSec = System.currentTimeMillis(); logger.info("end test, current time : {}", endTimeMilSec); logger.info("total run time : {} milSec", endTimeMilSec - startTimeMilSec);}private TryCatchFinallyTestClass tryCatchFinallyTestFun() {TryCatchFinallyTestClass testResult = new TryCatchFinallyTestClass(); testResult.setInfo("origin text before change"); try {logger.info("start try"); throw new Exception("test exception"); } catch (Exception e) {logger.info("start catch"); return testResult; } finally {logger.info("start finally"); testResult.setInfo("text change by finally"); logger.info("end finally"); }}private class TryCatchFinallyTestClass {private String info; public String getInfo() {return info; }public void setInfo(String info) {this.info = info; }}
基本思路:tryCatchFinallyFun()函数会按照try->catch->finally执行,在catch中return(在try中return原理应该一样,就不单独测试了)。在finally中修改return的值(为了防止系统自带类引起意外,建了一个测试类TryCatchFinallyTestClass存返回信息)。tryCatchFinallyTest调用测试函数,然后打印各个阶段的执行信息。测试结果如下:
17:19:41.821 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - start test, current time : 1505467181821
17:19:41.823 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - start try
17:19:41.823 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - start catch
17:19:41.823 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - start finally
17:19:41.823 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - end finally
17:19:41.823 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - return info : text change by finally
17:19:41.823 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - end test, current time : 1505467181823
17:19:41.823 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - total run time : 2 milSec
可以看到执行顺序:主程序->try->catch->finally->return->返回,而且最终返回的值被finally修改了。
进一步地研究一下:
在catch、finally中引发异常对return结果的影响
测试1:
如果在catch中有意引发一个exception会不会影响return的返回?
private TryCatchFinallyTestClass tryCatchFinallyTestFun() {TryCatchFinallyTestClass testResult = new TryCatchFinallyTestClass(); testResult.setInfo("origin text before change"); try {logger.info("start try"); throw new Exception("test exception"); } catch (Exception e) {logger.info("start catch"); testResult = new TryCatchFinallyTestClass(); testResult.getInfo().getBytes();//throws exception here logger.info("end catch"); return testResult; } finally {logger.info("start finally"); testResult = new TryCatchFinallyTestClass(); testResult.setInfo("text changed by finally"); logger.info("end finally"); }}
如果finally中不设置return, 果然抛出异常java.lang.NullPointerException
那我在finally中设置return会怎么样?设置return后结果:
17:51:38.243 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - start test, current time : 1505469098243
17:51:38.245 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - start try
17:51:38.245 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - start catch
17:51:38.245 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - start finally
17:51:38.245 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - end finally
17:51:38.245 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - return info : text changed by finally
17:51:38.245 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - end test, current time : 1505469098245
17:51:38.245 [main] INFO com.study.javaweb.test1.methodTest.JavaMethodTest - total run time : 2 milSec
catch被中断了,但是函数正常返回
测试2:
在catch中return但是在finally中引发异常,会不会影响返回
private TryCatchFinallyTestClass tryCatchFinallyTestFun() {TryCatchFinallyTestClass testResult = new TryCatchFinallyTestClass(); testResult.setInfo("origin text before change"); try {logger.info("start try"); throw new Exception("test exception"); } catch (Exception e) {logger.info("start catch"); testResult.setInfo("text changed by catch"); logger.info("end catch"); return testResult; } finally {logger.info("start finally");// testResult = new TryCatchFinallyTestClass();// testResult.setInfo("text changed by finally"); testResult = new TryCatchFinallyTestClass(); testResult.getInfo().getBytes();//throws exception here logger.info("end finally"); return testResult;
}
}
结果:java.lang.NullPointerException,异常并影响返回
运行环境:/leean950806/article/details/77994864