Java~try、catch、finally中 没有return 与 有return 的执行顺序总结

没有return

try无异常

  • try、finally的代码块是顺序执行
public class TestReturn {

    private static void test() {
        try {
            System.out.println("try");
        }catch (Exception e) {
            System.out.println("catch");
        } finally {
            System.out.println("finally");
        }
    }

    public static void main(String[] args) {
        test();
    }
}

在这里插入图片描述

try有异常

  • try、catch、finally的代码块是顺序执行
public class TestReturn {

    private static void test() {
        try {
            System.out.println("try");
            //设置一个异常
            int a = 2 / 0;
        }catch (Exception e) {
            System.out.println("catch");
        } finally {
            System.out.println("finally");
        }
    }

    public static void main(String[] args) {
        test();
    }
}

在这里插入图片描述

有return

try有return

  • 因为当try中带有return时,会先执行return前的代码,然后暂时保存需要return的信息,再执行finally或者catch中的代码,最后再通过return返回之前保存的信息。所以,这里方法返回的值是try中计算后的1,而非finally或者catch中计算后的2或3。
public class TestReturn {

    private static int test() {
        int num = 0;
        try {
            num++;
            System.out.println("try"+ " " + num);
            return num;
        }catch (Exception e) {
            num++;
            System.out.println("catch"+ " " + num);
        } finally {
            num++;
            System.out.println("finally"+ " " + num);
        }
        return num;
    }

    public static void main(String[] args) {
        System.out.println(test());
    }
}

在这里插入图片描述

  • ==但是必须要注意的是上面例子只是对于基本数据类型 如果是引用类型 存的不是变量本身,而是变量的地址,所以当finally或者catch通过地址改变了变量,还是会影响方法返回值的。
private static List<Integer> test2() {
        List<Integer> list = new ArrayList<>();
        try {
            list.add(1);
            System.out.println("try:" + list);
            return list;
        } catch (Exception e) {
            list.add(2);
            System.out.println("catch:" + list);
        } finally {
            list.add(3);
            System.out.println("finally:" + list);
        }
        return list;
    }

在这里插入图片描述

catch有return

  • catch中带return还有俩种情况 如果try中是有异常的 那么catch中return与try中一样,会先执行try的return前的代码,然后触发异常 忽略try中的return语句, 然后执行catch代码块中的代码 再执行catch中的return保存当前 i 的值,再执行finally中的代码,最后再通过return返回之前保存的信息。所以,这里方法返回的值是try、catch中累积计算后的2,而非finally中计算后的3。
public class TestReturn {

    private static int test() {
        int num = 0;
        try {
            num++;
            System.out.println("try"+ " " + num);
            int a = 2 / 0;
            return num;
        }catch (Exception e) {
            num++;
            System.out.println("catch"+ " " + num);
            return num;
        } finally {
            num++;
            System.out.println("finally"+ " " + num);
        }
    }


    public static void main(String[] args) {
        System.out.println(test());
    }
}

在这里插入图片描述

finally有return

  • 当finally中有return的时候,try和catch中的return会失效,在执行完finally的return之后,就不会再执行try中的return。这种写法,编译是可以编译通过的,但是编译器会给予警告,所以不推荐在finally中写return,这会破坏程序的完整性,而且一旦finally里出现异常,会导致catch中的异常被覆盖。
public class TestReturn {

    private static int test() {
        int num = 0;
        try {
            num++;
            System.out.println("try"+ " " + num);
            int a = 2 / 0;
            return num;
        }catch (Exception e) {
            num++;
            System.out.println("catch"+ " " + num);
            return num;
        } finally {
            num++;
            System.out.println("finally"+ " " + num);
            return num;
        }
    }


    public static void main(String[] args) {
        System.out.println(test());
    }
}

在这里插入图片描述

总结

  1. finally代码块总是会执行
  2. 如果是基本数据类型并且finally或者catch中没有return 即使catch或者finally对其数据进行修改 也不会对返回的数据有影响 但是如果是引用类型就会对返回的数据有影响
  3. finally中有return时,会直接在finally中退出,导致try、catch中的return失效。

猜你喜欢

转载自blog.csdn.net/Shangxingya/article/details/106660608
今日推荐