package test;
//jdk 1.8
public 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();}}}
详细解决方案
关于try-catch、throw、finally在异常时的执行顺序
热度:49 发布时间:2023-12-08 03:58:56.0
相关解决方案
- java中tr{} catch{}的语法作用,在什么时候用,该如何解决
- throw new CException;在vc6中没有关问题(vc++技术内幕中的一个例子里),但在vs2008里调试出错了!
- throw new Exception("不得为空");如何换行
- try.catch.求教解决思路
- throw new Exception("Message.")什么意思?该如何处理
- throw new ApplicationException("")解决方案
- 请教在一个try.catch.finally.结构中怎么认为制造错误
- 異常處理,catch(Exception ex)中的ex.Message怎樣在alert中調用到?解决思路
- 怎么在父窗口等待子窗口载入完成? try catch
- try catch throw php收集
- PHP里面如何用try…catch
- 对try. catch 的1点理解
- JS 应用try.catch
- JavaScript的例外处理(try.catch.finally)
- JS的try.catch.finally
- JavaScript的例外处置(try.catch.finally)
- 请教一个 try catch finally的有关问题
- try{}catch{}里面无法执行javascript脚本?解决方案
- 'try' without 'catch' or 'finally' 有关问题
- java中try-catch-finally中的return话语
- Myeclipse的tyry catch 如何让它自动生成
- 怎么自动生成try.catch
- throw throws try-catch 错误类型
- try-catch-finally的return 美混乱~
- 错误处理throws、throw、try catch这三者的区别
- Java错误之try,catch,finally,throw,throws
- TRIGER RAISE /CATCH 問題,该如何处理
- try catch finally 语句块,什么情况finally里的语句不会执行?解决方法
- 为何在用Thread.sleep()总在加try{}catch(){}语句
- throw new java.util.EmptyStackException();