Java 异常处理中的返回值在不同位置不同场景下是有一些差别的,这里需要格外注意

具体分以下两种场景:

  • 1 finally语句块没有return语句,即当代码执行到try或者catch语句块中的return语句时,相当于在本地保存了一份此时返回值的变量的值副本,然后按照try catch finally的运行机制去执行finally语句块,然后回过来将刚刚保存的副本数据返回

第一个例子为在try中没有异常,直接走到try语句块中的return场景

public class Test01 {
    public static void main(String[] args) {
        int results=new Test01().test(10,2);
        System.out.println("func results:"+results);
    }

    public int test(int a,int b)
    {
        int results=0;
        try
        {
            System.out.println("in try.....");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
        catch (Exception e)
        {
            System.out.println("in catch......");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
        finally {
            System.out.println("in finally");
            results+=1;
            System.out.println("results = "+results);
        }
    }
}

执行结果如下,最后函数返回的值是1,而不是2

in try.....
results = 1
in finally
results = 2
func results:1

第二个例子为try中出现了异常,但是在catch中走到了return语句

public class Test01 {
    public static void main(String[] args) {
        int results=new Test01().test(10,2);
        System.out.println("func results:"+results);
    }

    public int test(int a,int b)
    {
        int results=0;
        try
        {
            System.out.println("in try.....");
            results+=1;
            System.out.println("results = "+results);
            double d=1/0;
            return results;
        }
        catch (Exception e)
        {
            System.out.println("in catch......");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
        finally {
            System.out.println("in finally");
            results+=1;
            System.out.println("results = "+results);
        }
    }
}

返回值如下,即此时返回值是2而不是3

in try.....
results = 1
in catch......
results = 2
in finally
results = 3
func results:2
  • 2 finally语句块有return语句,此时即最终返回finally语句块中的的值,而try或者catch语句块中及时有return语句,也不会发生作用

下面的例子即try中能走到return语句,同样finally中也有return语句时候,此时返回finally语句块中的return的值

public class Test01 {
    public static void main(String[] args) {
        int results=new Test01().test(10,2);
        System.out.println("func results:"+results);
    }

    public int test(int a,int b)
    {
        int results=0;
        try
        {
            System.out.println("in try.....");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
        catch (Exception e)
        {
            System.out.println("in catch......");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
        finally {
            System.out.println("in finally");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
    }
}

执行结果如下,返回2,此时不是返回1了

in try.....
results = 1
in finally
results = 2
func results:2

下面的例子即在catch走到了return语句,同样在finally语句块中也走到了return语句,此时同样也是返回finally语句块中的值,catch语句块中的返回值失效

public class Test01 {
    public static void main(String[] args) {
        int results=new Test01().test(10,2);
        System.out.println("func results:"+results);
    }

    public int test(int a,int b)
    {
        int results=0;
        try
        {
            System.out.println("in try.....");
            results+=1;
            System.out.println("results = "+results);
            double d=1/0;
            return results;
        }
        catch (Exception e)
        {
            System.out.println("in catch......");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
        finally {
            System.out.println("in finally");
            results+=1;
            System.out.println("results = "+results);
            return results;
        }
    }
}

执行结果如下:返回3而不是2了

in try.....
results = 1
in catch......
results = 2
in finally
results = 3
func results:3

版权声明:本文为xiaojianghu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xiaojianghu/p/14318696.html