try-catch-finally块执行顺序

1.try/catch/finally 中没有return

public class B {
    public static void main(String[] args) {
        System.out.println("test:x="+test());  //step 1   //step 8 x:10
    }

    public static int test() {
        int x = 0; //step 2  x:0
        try {
            x = 1; //step 3
            System.out.println("try:x="+x); //step 4  x:1
        } catch (Exception e) {
            x = 2;
            System.out.println(x);
        } finally {
            x = 10;  //step 5
            System.out.println("finally:x="+x);  //step 6   x:10
        }
        return x; //step 7  x:10
    }
}

输出结果:
try:x=1
finally:x=10
test:x=10
按顺序执行,先执行try块,在没有异常的情况下,接下来执行finally块,随后return

2.try/catch 中有return

public class B {
    public static void main(String[] args) {
        System.out.println("test:x="+test()); //step 1    //step 9  x:1  
    }

    public static int test() {
        int x = 0; //step 2  x:0
        try {
            x = 1; //step 3
            System.out.println("try:x="+x); //step 4  x:1
            return x;  //step 5   x:1   //step 8  x:1
        } catch (Exception e) {
            x = 2;
            System.out.println(x);
            return x;
        } finally {
            x = 10;  //step 6
            System.out.println("finally:x="+x);  //step 7   x:10
        }
    }
}

输出结果:
try:x=1
finally:x=10
test:x=1
代码先执行try块,在执行finally块。finally块总会执行。try语句中的return返回的引用变量并不是try语句外定义的引用变量x,而是系统重新定义了一个局部引用x1,这个引用指向了引用x对应的值,也就是1,即使在finally语句中把引用x指向了值10,但是return已经不是x,而是x1,所以引用x的值和try语句中的返回值无关了

3.try/catch/finally 中有return

public class B {
    public static void main(String[] args) {
        System.out.println("test:x="+test());  //step 1    //step 9  x:10
    }

    public static int test() {
        int x = 0; //step 2  x:0
        try {
            x = 1; //step 3
            System.out.println("try:x="+x); //step 4  x:1
            return x;  //step 5   x:1  
        } catch (Exception e) {
            x = 2;
            System.out.println(x);
            return x;
        } finally {
            x = 10;  //step 6
            System.out.println("finally:x="+x);  //step 7   x:10
            return x;  //step 8  x:10
        }
    }
}

输出结果:
try:x=1
finally:x=10
test:x=10
结果是从finally块中返回。JVM忽略了try块中的return语句。但IDEA中会对finally块中return有黄色警告提示,原因如下:

4.try块有异常

public class B {
    public static void main(String[] args) {
        System.out.println("test:x="+test());  //step 1    //step 12  x:10
    }

    public static int test() {
        int x = 0; //step 2  x:0
        try {
            x = 1; //step 3
            Integer.parseInt(null);  //step 4
            System.out.println("try:x="+x);
            return x;  //step 5   x:1
        } catch (Exception e) {
            x = 2;   //step 6
            System.out.println("catch:x="+x);   //step 7   x:2
            return x;  //step 8  x:2
        } finally {
            x = 10;  //step 9
            System.out.println("finally:x="+x);  //step 10   x:10
            return x;  //step 11  x:10
        }
    }
}

输出结果:
catch:x=2
finally:x=10
test:x=10
finally中含return语句,导致try/catch中的异常信息被忽略,而我们最初使用try/catch就是为了捕获异常信息,所以这种情况与之产生矛盾,因此编译器会有警告提示

5.try/catch/finally 均有异常

public class B {
    public static void main(String[] args) {
        System.out.println("test:x="+test());  //step 1   
    }

    public static int test() {
        int x = 0; //step 2  x:0
        try {
            x = 1; //step 3
            Integer.parseInt(null);  //step 4
            System.out.println("try:x="+x);
            return x;  
        } catch (Exception e) {
            x = 2;   //step 5
            String.valueOf(null);  //step 6
            System.out.println("catch:x="+x);   
            return x;  
        } finally {
            x = 10;  //step 7
            int y = x/0;  //step 8
            System.out.println("finally:x="+x);  
        }
    }
}

输出结果:
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.wxj.newcode.java.B.test(B.java:22)
    at com.wxj.newcode.java.B.main(B.java:5)
如果finally发生异常,那么try/catch中额异常信息就会被忽略,达不到异常信息处理的目的

综上:
finally语句总会执行
如果try/catch中有return语句,finally中没有return语句,那么finally中修改数据(除集合类、静态变量、全局变量)对try/catch中返回的变量没有任何影响
在finally中使用return,会忽略try/catch中的返回语句,也会忽略try/catch中的异常
finally中如果发生异常,代码执行将会抛出finally中的异常信息

你可能感兴趣的:(Java)