003-try-catch-finally-return执行顺序问题

一、try-catch-finally-return执行顺序问题

0、原始执行顺序

try — > finally

try —> catch —> finally

1、try catch 中有 return,finally 中无 return,且 try 中无异常抛出

public int add(){
    int i = 1;
    try{
        i = i + 1;
        System.out.println("try: i = " + i);
        return i;
    }catch (Exception e){
        i = i + 2;
        System.out.println("exception: i = " + i);
        return i;
    }finally {
        i = i + 3;
        System.out.println("finally: i = " + i);
    }
}
/**
执行结果:
try: i = 2
finally: i = 5
2
*/

2、try catch finally中均有 return,try中无异常抛出

public int add(){
    int i = 1;
    try{
        i = i + 1;
        System.out.println("try: i = " + i);
        return i;
    }catch (Exception e){
        i = i + 2;
        System.out.println("exception: i = " + i);
        return i;
    }finally {
        i = i + 3;
        System.out.println("finally: i = " + i);
        return i;
    }
}
/**
返回结果5
try: i = 2
finally: i = 5
5
*/

3、try catch 中有 return,finally 中无 return,且 try 中有异常抛出

public int add(){
    int i = 1;
    try{
        i = i + 1;
        int a = i/0;
        System.out.println("try: i = " + i);
        return i;
    }catch (Exception e){
        i = i + 2;
        System.out.println("exception: i = " + i);
        return i;
    }finally {
        i = i + 3;
        System.out.println("finally: i = " + i);
    }
}
/**
返回结果4
exception: i = 4
finally: i = 7
4
*/

4、try catch finally中均有 return,try中有异常抛出

public int add(){
    int i = 1;
    try{
        i = i + 1;
        int a = i/0;
        System.out.println("try: i = " + i);
        return i;
    }catch (Exception e){
        i = i + 2;
        System.out.println("exception: i = " + i);
        return i;
    }finally {
        i = i + 3;
        System.out.println("finally: i = " + i);
        return i;
    }
}
/**
返回结果5
exception: i = 4
finally: i = 7
7
*/

5、try、catch 中有 return,try finally 中有异常,finally 中的异常有 return

public int add(){
    int i = 1;
    try{
        i = i + 1;
        int a = i/0;
        System.out.println("try: i = " + i);
        return i;
    }catch (Exception e){
        i = i + 2;
        System.out.println("exception: i = " + i);
        return i;
    }finally {
        i = i + 3;
        try {
            int b = i/0;
        } catch (Exception e) {
             i = i + 5;
             return i;
        }
        System.out.println("finally: i = " + i);
    }
}
/**
exception: i = 4
12
*/

6、try、catch 中有 return,try finally 中有异常,finally 中的异常无 return

public int add(){
    int i = 1;
    try{
        i = i + 1;
        int a = i/0;
        System.out.println("try: i = " + i);
        return i;
    }catch (Exception e){
        i = i + 2;
        System.out.println("exception: i = " + i);
        return i;
    }finally {
        i = i + 3;
        try {
            int b = i/0;
        } catch (Exception e) {
             i = i + 5;
        }
        System.out.println("finally: i = " + i);
    }
}
/**
exception: i = 4
finally: i = 12
4
*/

总结

  • finally 的代码总会被执行
  • try、catch 中有 return 的时候,也会执行 finally。return 的时候,要注意返回值是否会受到 finally 中代码的影响。
  • finally 中有 return 的时候,会直接在 finally 中退出,导致 try、catch中的 return 失效。
  • finally 中如果有异常的时候,且存在 return 的时候,catch 的 return 会被覆盖。
上一篇:003-Android—Activity练习题


下一篇:嵌入式100题(003):什么时候用进程,什么时候用线程