• Java中 return 和finally


    1. 最简单的情形

        public void main(){
            String s = test();
            System.out.println("s=[" + s + "]");
        }
        
        public String test(){
            String s = "";
            try{
                System.out.println("In try");
                s += "try," ;
            }catch(Exception ex){
                System.out.println("In catch");
            }finally{
                s += "finally,";
                System.out.println("In finally");
            }
            s+="other";
            return s;
        }

    输出的结果是:

    In try
    In finally
    s=[try,finally,other]

    2. 在try中加上return,那么结果是

        public String test(){
            String s = "";
            try{
                System.out.println("In try");
                s += "try," ;
                return s; //加上return
            }catch(Exception ex){
                System.out.println("In catch");
            }finally{
                s += "finally,";
                System.out.println("In finally");
            }
            s+="other";
            return s;
        }

    结果是

    In try
    In finally
    s=[try,]

    说明虽然会执行finally语句,但是return的结果已经准备好了。然后才执行finally,在执行finally之前,会将返回的结果存储到不同于s的变量中,finally执行成功之后,才会返回对应的结果。因此即使在finally中对结果进行修改,也不会修改返回的结果。

    3. 如果在catch中return会怎么样呢?

        public String test(){
            String s = "";
            try{
                System.out.println("In try");
                s += "try," ;
                throwException();
            }catch(NullPointerException ex){
                System.out.println("In catch");
                s += "catch,";
                return s;
            }finally{
                s += "finally,";
                System.out.println("In finally");
            }
            s+="other";
            return s;
        }
        
        public void throwException() throws NullPointerException{
            throw new NullPointerException();
        }

    执行结果是:

    In try
    In catch
    In finally
    s=[try,catch,]

    可见道理是一样的,也就是不管在try还是在catch中的语句中return的结果,即使在finally中进行修改,都不会改变结果。

    4. return 语句先执行,还是finally先执行呢?

        public String test(){
            String s = "";
            try{
                
                System.out.println("In try");
                s += "try," ;
                throwException();
            }catch(NullPointerException ex){
                System.out.println("In catch");
                s += "catch,";
                return s += testReturn();
            }finally{
                s += "finally,";
                System.out.println("In finally");
            }
            s+="other";
            return s;
        }
        
        public String testReturn(){
            System.out.println("In Return");
            return "return,";
        }
        
        public void throwException() throws NullPointerException{
            throw new NullPointerException();
        }

    结果是:

    In try
    In catch
    In Return
    In finally
    s=[try,catch,return,]

    也就是,return还是要比finally先执行,但是这时函数还没急着返回,只有把finally执行完才会返回。

    总结的结果是: 

    1. 不管是否有异常,finally都会执行

    2. 即使try或catch中有return语句,finally仍然会执行

    3. finally中语句是在return语句之前执行的,即return语句执行就决定了返回的结果

    4. return语句执行完如果还有finally块,还会执行finally块,但是无法修改return语句返回的结果

    参考资料:

    http://www.cnblogs.com/ybwang/archive/2010/10/10/1847269.html

  • 相关阅读:
    nexus docker 私有镜像处理
    nexus 使用Raw Repositories 进行maven site 发布
    nexus && minio s3 存储私有镜像
    spring boot 使用spring.resources.static-locations 分离系统模版&&资源文件
    Tencent Server Web 安装试用
    docker could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network
    Tencent Server Web(TSW) 腾讯开源的nodejs 基础设施
    Stream Processing 101: From SQL to Streaming SQL in 10 Minutes
    13 Stream Processing Patterns for building Streaming and Realtime Applications
    Siddhi cep java 集成简单使用
  • 原文地址:https://www.cnblogs.com/longshaohang/p/3748076.html
Copyright © 2020-2023  润新知