JAVA異常處理之finally中最好不要使用return


finally 語句塊中, 最好不要使用return, 否則會造成已下后果;

1, 如果catch塊中捕獲了異常, 並且在catch塊中將該異常throw給上級調用者進行處理, 但finally中return了, 那么catch塊中的throw就失效了, 上級方法調用者是捕獲不到異常的. 見demo如下:

 1 public class TestException {
 2     public TestException() {
 3     }
 4 
 5     boolean testEx() throws Exception {
 6         boolean ret = true;
 7         try {
 8             ret = testEx1();
 9         } catch (Exception e) {
10             System.out.println("testEx, catch exception");
11             ret = false;
12             throw e;
13         } finally {
14             System.out.println("testEx, finally; return value=" + ret);
15             return ret;
16         }
17     }
18 
19     boolean testEx1() throws Exception {
20         boolean ret = true;
21         try {
22             ret = testEx2();
23             if (!ret) {
24                 return false;
25             }
26             System.out.println("testEx1, at the end of try");
27             return ret;
28         } catch (Exception e) {
29             System.out.println("testEx1, catch exception");
30             ret = false;
31             throw e;
32         } finally {
33             System.out.println("testEx1, finally; return value=" + ret);
34             return ret;
35         }
36     }
37 
38     boolean testEx2() throws Exception {
39         boolean ret = true;
40         try {
41             int b = 12;
42             int c;
43             for (int i = 2; i >= -2; i--) {
44                 c = b / i;
45                 System.out.println("i=" + i);
46             }
47             return true;
48         } catch (Exception e) {
49             System.out.println("testEx2, catch exception");
50             ret = false;
51             throw e;
52         } finally {
53             System.out.println("testEx2, finally; return value=" + ret);
54             return ret;
55         }
56     }
57 
58     public static void main(String[] args) {
59         TestException testException1 = new TestException();
60         try {
61             testException1.testEx();
62         } catch (Exception e) {
63             e.printStackTrace();
64         }
65     }
66 }
View Code

該方法的最終執行結果如下:

i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false

 

盡管testEx2中的catch拋出異常, 但因為finally中return了, 那么testEx1中是捕獲不到異常的, 所以testEx1中的catch塊是不會執行的.

 

2, 如果在finally里的return之前執行了其它return , 那么最終的返回值是finally中的return:

 

public class TestException3 {
    public static int x = 3;
    int testEx() throws Exception {
        try {
            x = 4;
            return x;
        } catch (Exception e) {

        } finally {
            x = 5;
            return x;
        }
    }
    public static void main(String[] args) {
        TestException3 testException3 = new TestException3();
        try {
            int a = testException3.testEx();
            System.out.println(a);
            System.out.println(x);
        } catch (Exception e) {
        }
    }
}
View Code

輸出結果為5, 5

 

如果finally中的return被注釋掉, 那么輸出結果是4, 5


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM