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


    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
  • 相关阅读:
    「移动开发云端新模式探索实践」征文活动
    为数据赋能:腾讯TDSQL分布式金融级数据库前沿技术
    腾讯刘金明:腾讯云 EB 级对象存储架构深度剖析及实践
    腾讯冯宇彦:基于大数据与人工智能的智慧交通云
    腾讯毛华:智能交互,AI助力下的新生态
    腾讯聂晶:数据资产助力企业发展
    2018云+未来峰会圆桌面对面:以网络安全之能,造国之重器
    全景解析腾讯云安全:从八大领域输出全链路智慧安全能力
    为 “超级大脑”构建支撑能力,腾讯云聚焦AI技术落地
    web service介绍
  • 原文地址:https://www.cnblogs.com/wobuchifanqie/p/12764337.html
Copyright © 2020-2023  润新知