美文网首页
try-catch-finally

try-catch-finally

作者: 城堡下的晚祷 | 来源:发表于2018-10-17 11:31 被阅读0次

    (1)try中代码存在异常,先执行try中异常行之前的代码,再执行catch中代码,最后执行finally中代码。

    
      public static void main(String args[]) {
            try {
                System.out.println("0000000000");
                int a = 3 / 0;
                System.out.println("1111111111");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("2222222222");
            } finally {
                System.out.println("3333333333");
            }
    
        }
    ---------------------------------------------------------------
      0000000000
      java.lang.ArithmeticException: / by zero
      2222222222
      3333333333
        at Test.main(Test.java:6)
    
    

    (2)当try中不存在异常的时候,先执行try中代码,再执行finally中代码,catch中代码不会被执行。

     
     public static void main(String args[]) {
            try {
                System.out.println("0000000000");
                int a = 3 / 1;
                System.out.println("1111111111");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("2222222222");
            } finally {
                System.out.println("3333333333");
            }
    
        }
    ------------------------------------------------------------
      0000000000
      1111111111
      3333333333
    
    

    (3)当需要添加多个catch时,要把异常范围小的放在前面,范围大的放在后面,Exception这个异常的根类一定要刚在最后一个catch里面

    用catch捕获异常的时候,会根据catch(****)中的内容逐个执行,当发现到try中产生的异常和catch内的异常相同时候就会停止。否则会继续向下执行。

    
        public static void main(String args[]) {
            try {
                int a = 3 / 0;
            }catch (NullPointerException e) {
                e.printStackTrace();
                System.out.println("000000000000");
            } catch (ArithmeticException e) {
                e.printStackTrace();
                System.out.println("111111111111");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("222222222222");
            } finally {
            }
    
        }
    ------------------------------------------------------------
        java.lang.ArithmeticException: / by zero
        111111111111
            at com.zk.e.main(e.java:13)
    

    (4)当try和catch中有return,finally中无return
    存在异常时候,返回catch中return;
    不存在异常的时候,返回try中return;

    finally中的代码始终会执行,即使try,catch里面有return。

    
        public static void main(String args[]) {
            int c = test();
            System.out.println(c);
    
        }
    
        private static int test() {
            try {
                int a = 3 / 0;
                return 0;
            } catch (Exception e) {
                e.printStackTrace();
                return 1;
            } finally {
                System.out.println("****************");
            }
        }
    ------------------------------------------------------
    java.lang.ArithmeticException: / by zero
    ****************
    1
    

    (5)当try ,catch ,finally中都存在return,不管是否存在异常返回finally中return的值.

    当finall块中包含return语句时,Eclipse会给出警告“finally block does not complete normally”,原因分析如下:

    1、finally块中的return语句会覆盖前面的return语句(try块、catch块中的return语句),所以如果finally块中有return语句,Eclipse编译器会报警告“finally block does not complete normally”。

    2、如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,而是会得到finally块的返回值,并且不会捕获异常。

    
        public static void main(String args[]) {
            int c = test();
            System.out.println(c);
    
        }
    
        private static int test() {
            try {
                int a = 3 / 0;
                return 0;
            } catch (Exception e) {
                e.printStackTrace();
                return 1;
            } finally {
                return 2;
            }
        }
    -------------------------------------------
      java.lang.ArithmeticException: / by zero
          at Test.test(Test.java:11)
          at Test.main(Test.java:4)
      2
    

    (6)当try或catch中return一个变量,并且该变量在finally中值发生变化,方法返回值不受finally中值改变的影响。

         //不存在异常
        private static int Test() {
            int x = 0;
            try {
                x = 2 / 1;
                return x;
            } catch (Exception e) {
                e.printStackTrace();
                x = 4;
                return x;
            } finally {
                x = 5;
            }
        }
    
        public static void main(String[] args) {
            System.out.println(Test());
        }
    --------------------------------------------------
       2
    
      代码的行为如下:
      1.把try中返回值保存到局部变量中
      2.执行jsr指令跳到finally语句里执行
      3.执行完finally语句后,返回之前保存在局部变量表里的值
    
    
        //存在异常
        private static int Test() {
            int x = 0;
            try {
                x = 2 / 0;
                return x = 3;
            } catch (Exception e) {
                x = 4;
                return x;
            } finally {
                x = 5;
            }
        }
    
        public static void main(String[] args) {
            System.out.println(Test());
        }
    --------------------------------------------------
      java.lang.ArithmeticException: / by zero
          at com.zk.e.Test(e.java:14)
          at com.zk.e.main(e.java:26)
      4
    

    相关文章

      网友评论

          本文标题:try-catch-finally

          本文链接:https://www.haomeiwen.com/subject/jzrlzftx.html