关于try-catch、throw、finally在异常时的执行顺序
来源:互联网 发布:淘宝客违规推广 编辑:程序博客网 时间:2024/06/05 11:40
package test;//jdk 1.8public class TestException1 {/** * catch中的return和throw是不能共存的(无论谁先谁后都编译不通过) * 如果只throw e,则必须try-catch捕捉异常e或用throws抛出异常e * 如果只return ,则在catch段正常返回值 */int testEx0(){boolean ret = true;try {int c = 12 / 0;System.out.println("testEx,successfully");return 0;} catch (Exception e) {System.out.println("testEx, catch exception");ret = false;return -1;throw e;}}/** * 在finally中的return和throw是不能共存的(无论谁先谁后都编译不通过) * 如果只throw e,则必须try-catch捕捉异常e或用throws抛出异常e * 如果只return ,则在catch段正常返回值 */int testEx00(){int ret = 0;try {int c = 12 / 0;System.out.println("testEx,successfully");return ret;} catch (Exception e) {System.out.println("testEx, catch exception");ret = -1;}finally{ret = 1;System.out.println("testEx, finally; return value=" + ret);throw e;return ret;}}/** * 结果: testEx, catch exceptiontestEx, finally; return value=falsefalse结论:在finally里没有return的时候:先执行finally的语句,再执行catch的return */boolean testEx01(){boolean ret = true;try {int c = 12 / 0;System.out.println("testEx,successfully");return true;} catch (Exception e) {System.out.println("testEx, catch exception");ret = false;return ret;}finally{System.out.println("testEx, finally; return value=" + ret);}}/** * 结果: * testEx, catch exceptiontestEx, finally; return value=11结论:在finally里有return的时候:先执行finally的语句和return,忽略catch的return */int testEx02(){int ret = 0;try {int c = 12 / 0;System.out.println("testEx,successfully");return ret;} catch (Exception e) {ret = -1;System.out.println("testEx, catch exception");return ret;}finally{ret = 1;System.out.println("testEx, finally; return value=" + ret);return ret;}}/** * 编译能通过, * 但运行时抛异常(当然也没有返回值) * @return */boolean testEx03(){boolean ret = true;try {int c = 12 / 0;System.out.println("testEx,successfully");return true;} catch (Exception e) {System.out.println("testEx, catch exception");ret = false;throw e;}finally{System.out.println("testEx, finally; return value=" + ret);}}/** * 编译不能通过(必须加throws主动抛异常,或try-catch捕捉,) * 但运行时抛异常(当然也没有返回值) * @return * @throws Exception */boolean testEx031(){boolean ret = true;try {int c = 12 / 0;System.out.println("testEx,successfully");return true;} catch (Exception e) {System.out.println("testEx, catch exception");ret = false;throw new Exception(e);}finally{System.out.println("testEx, finally; return value=" + ret);}}/** * 结果: * testEx, catch exceptiontestEx, finally; return value=11 结论:函数在finally里正常返回return的值,无异常,显然catch中的throw被忽略 */int testEx04(){int ret = 0;try {int c = 12 / 0;System.out.println("testEx,successfully");return ret;} catch (Exception e) {System.out.println("testEx, catch exception");ret = -1;throw e;}finally{ret = 1;System.out.println("testEx, finally; return value=" + ret);return ret;}}public static void main(String[] args) {try {System.out.println(new TestException1().testEx0());//System.out.println(new TestException1().testEx00());//System.out.println(new TestException1().testEx01());//System.out.println(new TestException1().testEx02());//System.out.println(new TestException1().testEx03());//System.out.println(new TestException1().testEx031());//System.out.println(new TestException1().testEx04());} catch (Exception e) {e.printStackTrace();}}}
0 0
- 关于try-catch、throw、finally在异常时的执行顺序
- 关于try-catch、throw、finally在异常时的执行顺序
- try-catch-finally中throw,return时的执行顺序
- try ... catch中Throw异常后的执行顺序
- 捕捉异常中try、catch和finally的执行顺序
- 异常处理及try/catch/finally/return 的执行顺序:
- java中异常 try,catch,finally在不同情况下的执行顺序
- 关于java中try catch finally的执行顺序
- 关于Java中try-catch-finally-return的执行顺序
- 关于Java中try-catch-finally-return的执行顺序
- 关于try-catch-finally-return语句的执行顺序
- 关于Java中try-catch-finally-return的执行顺序
- 关于try、catch、finally中return的执行顺序
- try, catch, finally, return的执行顺序
- try、catch、finally、return的执行顺序
- try、catch、finally、return的执行顺序
- try catch finally return 的执行顺序
- try、catch、finally、return的执行顺序
- spark集群1.6.2升级为2.0.2
- maven相关
- linux初学使用技巧
- php获取目录下的所有文件
- Leetcode 46. Permutations
- 关于try-catch、throw、finally在异常时的执行顺序
- mysql分组取每组前几条记录(排名) 附group by与order by的研究
- android签名的应用-- 禁止未经授权签名的apk安装
- 【DVB】【Hbbv】ETSI发布最新版HbbTV标准
- git 提交代码模板的配置
- Python动态更新服务器ip到dnspod
- LeetCode 3Sum
- MySQL——load语句导入数据
- MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建