網(wǎng)上有很多人探討Java中異常捕獲機(jī)制try...catch...finally塊中的finally語句是不是一定會被執(zhí)行?很多人都說不是,當(dāng)然他們的回答是正確的,經(jīng)過我試驗(yàn),至少有兩種情況下finally語句是不會被執(zhí)行的:
(1)try語句沒有被執(zhí)行到,如在try語句之前就返回了,這樣finally語句就不會執(zhí)行,這也說明了finally語句被執(zhí)行的必要而非充分條件是:相應(yīng)的try語句一定被執(zhí)行到。
(2)在try塊中有System.exit(0);這樣的語句,System.exit(0);是終止Java虛擬機(jī)JVM的,連JVM都停止了,所有都結(jié)束了,當(dāng)然finally語句也不會被執(zhí)行到。
當(dāng)然還有很多人探討Finally語句的執(zhí)行與return的關(guān)系,頗為讓人迷惑,不知道finally語句是在try的return之前執(zhí)行還是之后執(zhí)行?我也是一頭霧水,我覺得他們的說法都不正確,我覺得應(yīng)該是:finally語句是在try的return語句執(zhí)行之后,return返回之前執(zhí)行。這樣的說法有點(diǎn)矛盾,也許是我表述不太清楚,下面我給出自己試驗(yàn)的一些結(jié)果和示例進(jìn)行佐證,有什么問題歡迎大家提出來。
1. finally語句在return語句執(zhí)行之后return返回之前執(zhí)行的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public class FinallyTest1 { public static void main(String[] args) { System.out.println(test1()); } public static int test1() { int b = 20 ; try { System.out.println( "try block" ); return b += 80 ; } catch (Exception e) { System.out.println( "catch block" ); } finally { System.out.println( "finally block" ); if (b > 25 ) { System.out.println( "b>25, b = " + b); } } return b; } } |
運(yùn)行結(jié)果是:
1
2
3
4
|
try block finally block b> 25 , b = 100 100 |
說明return語句已經(jīng)執(zhí)行了再去執(zhí)行finally語句,不過并沒有直接返回,而是等finally語句執(zhí)行完了再返回結(jié)果。
如果覺得這個例子還不足以說明這個情況的話,下面再加個例子加強(qiáng)證明結(jié)論:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class FinallyTest1 { public static void main(String[] args) { System.out.println(test11()); } public static String test11() { try { System.out.println( "try block" ); return test12(); } finally { System.out.println( "finally block" ); } } public static String test12() { System.out.println( "return statement" ); return "after return" ; } } |
運(yùn)行結(jié)果為:
1
2
3
4
|
try block return statement finally block after return |
說明try中的return語句先執(zhí)行了但并沒有立即返回,等到finally執(zhí)行結(jié)束后再
這里大家可能會想:如果finally里也有return語句,那么是不是就直接返回了,try中的return就不能返回了?看下面。
2. finally塊中的return語句會覆蓋try塊中的return返回。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public class FinallyTest2 { public static void main(String[] args) { System.out.println(test2()); } public static int test2() { int b = 20 ; try { System.out.println( "try block" ); return b += 80 ; } catch (Exception e) { System.out.println( "catch block" ); } finally { System.out.println( "finally block" ); if (b > 25 ) { System.out.println( "b>25, b = " + b); } return 200 ; } // return b; } } |
運(yùn)行結(jié)果是:
1
2
3
4
|
try block finally block b> 25 , b = 100 200 |
這說明finally里的return直接返回了,就不管try中是否還有返回語句,這里還有個小細(xì)節(jié)需要注意,finally里加上return過后,finally外面的return b就變成不可到達(dá)語句了,也就是永遠(yuǎn)不能被執(zhí)行到,所以需要注釋掉否則編譯器報錯。
這里大家可能又想:如果finally里沒有return語句,但修改了b的值,那么try中return返回的是修改后的值還是原值?看下面。
3. 如果finally語句中沒有return語句覆蓋返回值,那么原來的返回值可能因?yàn)閒inally里的修改而改變也可能不變。
測試用例1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public class FinallyTest3 { public static void main(String[] args) { System.out.println(test3()); } public static int test3() { int b = 20 ; try { System.out.println( "try block" ); return b += 80 ; } catch (Exception e) { System.out.println( "catch block" ); } finally { System.out.println( "finally block" ); if (b > 25 ) { System.out.println( "b>25, b = " + b); } b = 150 ; } return 2000 ; } } |
運(yùn)行結(jié)果是:
1
2
3
4
|
try block finally block b> 25 , b = 100 100 |
測試用例2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import java.util.*; public class FinallyTest6 { public static void main(String[] args) { System.out.println(getMap().get( "KEY" ).toString()); } public static Map<String, String> getMap() { Map<String, String> map = new HashMap<String, String>(); map.put( "KEY" , "INIT" ); try { map.put( "KEY" , "TRY" ); return map; } catch (Exception e) { map.put( "KEY" , "CATCH" ); } finally { map.put( "KEY" , "FINALLY" ); map = null ; } return map; } } |
運(yùn)行結(jié)果是:
FINALLY
為什么測試用例1中finally里的b = 150;并沒有起到作用而測試用例2中finally的map.put("KEY", "FINALLY");起了作用而map = null;卻沒起作用呢?這就是Java到底是傳值還是傳址的問題了,具體請看精選30道Java筆試題解答,里面有詳細(xì)的解答,簡單來說就是:Java中只有傳值沒有傳址,這也是為什么map = null這句不起作用。這同時也說明了返回語句是try中的return語句而不是 finally外面的return b;這句,不相信的話可以試下,將return b;改為return 294,對原來的結(jié)果沒有一點(diǎn)影響。
這里大家可能又要想:是不是每次返回的一定是try中的return語句呢?那么finally外的return b不是一點(diǎn)作用沒嗎?請看下面。
4. try塊里的return語句在異常的情況下不會被執(zhí)行,這樣具體返回哪個看情況。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public class FinallyTest4 { public static void main(String[] args) { System.out.println(test4()); } public static int test4() { int b = 20 ; try { System.out.println( "try block" ); b = b / 0 ; return b += 80 ; } catch (Exception e) { b += 15 ; System.out.println( "catch block" ); } finally { System.out.println( "finally block" ); if (b > 25 ) { System.out.println( "b>25, b = " + b); } b += 50 ; } return 204 ; } } |
運(yùn)行結(jié)果是:
1
2
3
4
5
|
try block catch block finally block b> 25 , b = 35 85 |
這里因 為在return之前發(fā)生了除0異常,所以try中的return不會被執(zhí)行到,而是接著執(zhí)行捕獲異常的catch 語句和最終的finally語句,此時兩者對b的修改都影響了最終的返回值,這時return b;就起到作用了。當(dāng)然如果你這里將return b改為return 300什么的,最后返回的就是300,這毋庸置疑。
這里大家可能又有疑問:如果catch中有return語句呢?當(dāng)然只有在異常的情況下才有可能會執(zhí)行,那么是在finally之前就返回嗎?看下面。
5. 當(dāng)發(fā)生異常后,catch中的return執(zhí)行情況與未發(fā)生異常時try中return的執(zhí)行情況完全一樣。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public class FinallyTest5 { public static void main(String[] args) { System.out.println(test5()); } public static int test5() { int b = 20 ; try { System.out.println( "try block" ); b = b / 0 ; return b += 80 ; } catch (Exception e) { System.out.println( "catch block" ); return b += 15 ; } finally { System.out.println( "finally block" ); if (b > 25 ) { System.out.println( "b>25, b = " + b); } b += 50 ; } //return b; } } |
運(yùn)行結(jié)果如下:
1
2
3
4
5
|
try block catch block finally block b> 25 , b = 35 35 |
說明了發(fā)生異常后,catch中的return語句先執(zhí)行,確定了返回值后再去執(zhí)行finally塊,執(zhí)行完了catch再返回,finally里對b的改變對返回值無影響,原因同前面一樣,也就是說情況與try中的return語句執(zhí)行完全一樣。
最后總結(jié):finally塊的語句在try或catch中的return語句執(zhí)行之后返回之前執(zhí)行且finally里的修改語句可能影響也可能不影響try或catch中 return已經(jīng)確定的返回值,若finally里也有return語句則覆蓋try或catch中的return語句直接返回。
以上這篇淺談Java finally語句到底是在return之前還是之后執(zhí)行(必看篇)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。