Java深入学习24:try catch finally执行和返回逻辑

 

先说结论:

  一般逻辑,先执行try语句;如果有异常,则继续执行catch语句;如果有finally语句,则执行finally语句;

  任何执行try 或者catch中的return语句之前,如果finally存在的,都会先执行finally语句;

  如果finally中有return语句,那么程序就在finally中return了,所以finally中的return是一定会被return的,否则

 

源码示例:

public class TryCatchTest {

    public static String testNormalWithFinally(){

        try {
            int i = 0/1;
            System.out.println("this is try block");
            return "return try";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("this is catch block");
            return "return catch";
        } finally {
            System.out.println("this is finally block");
            return "return finally ";
        }

    }

    public static String testExceptionWithFinally(){

        try {
            int j = 1/0;
            System.out.println("this is ctry block");
            return "return try";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("this is catch block");
            return "return catch";
        } finally {
            System.out.println("this is finally block");
            return "return finally ";
        }

    }
    public static String testNormalWithoutFinallyReturn(){

        try {
            int i = 0/1;
            System.out.println("this is try block");
            return "return try";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("this is catch block");
            return "return catch";
        } finally {
            System.out.println("this is finally block");
        }

    }
    public static String testExceptionWithoutFinallyReturn(){

        try {
            int j = 1/0;
            System.out.println("this is ctry block");
            return "return try";
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("this is catch block");
            return "return catch";
        } finally {
            System.out.println("this is finally block");
        }

    }

    public static void main(String[] args) {
        System.out.println("-------------testNormalWithFinally-------------------");
        System.out.println(testNormalWithFinally());
        System.out.println("----------------testExceptionWithFinally----------------");
        System.out.println(testExceptionWithFinally());
        System.out.println("---------------testNormalWithoutFinallyReturn-----------------");
        System.out.println(testNormalWithoutFinallyReturn());
        System.out.println("--------------testExceptionWithoutFinallyReturn------------------");
        System.out.println(testExceptionWithoutFinallyReturn());
    }
}
--------------------------------日志输出---------------------------------------------
-------------testNormalWithFinally-------------------
java.lang.ArithmeticException: / by zero
this is try block
    at interview.trycatchfinally.TryCatchTest.testExceptionWithFinally(TryCatchTest.java:32)
this is finally block
return finally 
    at interview.trycatchfinally.TryCatchTest.main(TryCatchTest.java:80)
----------------testExceptionWithFinally----------------
java.lang.ArithmeticException: / by zero
this is catch block
this is finally block
return finally 
    at interview.trycatchfinally.TryCatchTest.testExceptionWithoutFinallyReturn(TryCatchTest.java:63)
---------------testNormalWithoutFinallyReturn-----------------
this is try block
    at interview.trycatchfinally.TryCatchTest.main(TryCatchTest.java:84)
this is finally block
return try
--------------testExceptionWithoutFinallyReturn------------------
this is catch block
this is finally block
return catch

 

对应的字节码理解

public static java.lang.String testNormalWithFinally();
    Code:
       0: iconst_0
       1: istore_0
       2: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       5: ldc           #3                  // String this is try block
       7: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      10: ldc           #5                  // String return try
      12: astore_1
      13: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      16: ldc           #6                  // String this is finally block
      18: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      21: ldc           #7                  // String return finally
      23: areturn
      24: astore_0
      25: aload_0
      26: invokevirtual #9                  // Method java/lang/Exception.printStackTrace:()V
      29: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      32: ldc           #10                 // String this is catch block
      34: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      37: ldc           #11                 // String return catch
      39: astore_1
      40: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      43: ldc           #6                  // String this is finally block
      45: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      48: ldc           #7                  // String return finally
      50: areturn
      51: astore_2
      52: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      55: ldc           #6                  // String this is finally block
      57: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      60: ldc           #7                  // String return finally
      62: areturn
    Exception table:
       from    to  target type
           0    13    24   Class java/lang/Exception
           0    13    51   any
          24    40    51   any

 

posted on 2020-04-23 22:36  我不吃番茄  阅读(381)  评论(0编辑  收藏  举报