java的异常处理(一)

  1. package Test;  
  2.   
  3. public class TestException {  
  4.     public TestException() {  
  5.     }  
  6.   
  7.     boolean testEx() throws Exception {  
  8.         boolean ret = true;  
  9.         try {  
  10.             ret = testEx1();  
  11.         } catch (Exception e) {  
  12.             System.out.println("testEx, catch exception");  
  13.             ret = false;  
  14.             throw e;  
  15.         } finally {  
  16.             System.out.println("testEx, finally; return value=" + ret);  
  17.             return ret;  
  18.         }  
  19.     }  
  20.   
  21.     boolean testEx1() throws Exception {  
  22.         boolean ret = true;  
  23.         try {  
  24.             ret = testEx2();  
  25.             if (!ret) {  
  26.                 return false;  
  27.             }  
  28.             System.out.println("testEx1, at the end of try");  
  29.             return ret;  
  30.         } catch (Exception e) {  
  31.             System.out.println("testEx1, catch exception");  
  32.             ret = false;  
  33.             throw e;  
  34.         } finally {  
  35.             System.out.println("testEx1, finally; return value=" + ret);  
  36.             return ret;  
  37.         }  
  38.     }  
  39.   
  40.     boolean testEx2() throws Exception {  
  41.         boolean ret = true;  
  42.         try {  
  43.             int b = 12;  
  44.             int c;  
  45.             for (int i = 2; i >= -2; i--) {  
  46.                 c = b / i;  
  47.                 System.out.println("i=" + i);  
  48.             }  
  49.             return true;  
  50.         } catch (Exception e) {  
  51.             System.out.println("testEx2, catch exception");  
  52.             ret = false;  
  53.             throw e;  
  54.         } finally {  
  55.             System.out.println("testEx2, finally; return value=" + ret);  
  56.             return ret;  
  57.         }  
  58.     }  
  59.   
  60.     public static void main(String[] args) {  
  61.         TestException testException1 = new TestException();  
  62.         try {  
  63.             testException1.testEx();  
  64.         } catch (Exception e) {  
  65.             e.printStackTrace();  
  66.         }  
  67.     }  

  1. }  
  2. 答案:
  3. i=2
    i=1
    testEx2, catch exception
    testEx2, finally; return value=false
    testEx1, finally; return value=false
    testEx, finally; return value=false


    说明return语句已经执行了再去执行finally语句,不过并没有直接返回,而是等finally语句执行完了再返回结果
    1. public class FinallyTest1 {  
    2.   
    3.     public static void main(String[] args) {  
    4.           
    5.         System.out.println(test11());  
    6.     }  
    7.       
    8.     public static String test11() {  
    9.         try {  
    10.             System.out.println("try block");  
    11.   
    12.            return test12();  
    13.       } finally {  
    14.            System.out.println("finally block");  
    15.        }  
    16.   }  
    17.   
    18.   public static String test12() {  
    19.        System.out.println("return statement");  
    20.   
    21.        return "after return";  
    22.    }  
    23.       
    24. }
         说明try中的return语句先执行了但并没有立即返回,等到finally执行结束后再执行
finally块中的return语句会覆盖try块中的return返回。
  1. public class FinallyTest2 {  
  2.   
  3.     public static void main(String[] args) {  
  4.   
  5.         System.out.println(test2());  
  6.     }  
  7.   
  8.     public static int test2() {  
  9.         int b = 20;  
  10.   
  11.         try {  
  12.             System.out.println("try block");  
  13.   
  14.             return b += 80;  
  15.         } catch (Exception e) {  
  16.   
  17.             System.out.println("catch block");  
  18.         } finally {  
  19.   
  20.             System.out.println("finally block");  
  21.   
  22.             if (b > 25) {  
  23.                 System.out.println("b>25, b = " + b);  
  24.             }  
  25.   
  26.             return 200;  
  27.         }  
  28.   
  29.         // return b;  
  30.     }  
  31.   
  32. }  
这说明finally里的return直接返回了,就不管try中是否还有返回语句,这里还有个小细节需要注意,finally里加上return过后,finally外面的return b就变成不可到达语句了,也就是永远不能被执行到,所以需要注释掉否则编译器报错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值