Java基礎(chǔ)之finally語句與return語句詳解
一、return語句執(zhí)行順序
finally語句是在return語句執(zhí)行之后,return語句返回之前執(zhí)行的
package exception; public class Demo06 { public static void main(String[] args) { System.out.println(func()); } public static int func(){ int a = 10; try{ System.out.println("try中的代碼塊"); return a += 10; }catch (Exception e){ System.out.println("catch中的代碼塊"); }finally { System.out.println("finally中的代碼塊"); if(a > 10){ System.out.println("a > 10,"+"a="+a); } } return a += 50; } }
運(yùn)行結(jié)果:
try中的代碼塊
finally中的代碼塊
a > 10,a=20
20
注意:
a > 10,a=20的結(jié)果說明了return a += 10已經(jīng)執(zhí)行了,但是沒有直接返回,而是先去執(zhí)行finally語句的內(nèi)容,然后再去返回結(jié)果
二、覆蓋問題
finally塊中的return語句會覆蓋try塊的return返回
package exception; public class Demo07 { public static void main(String[] args) { System.out.println(func()); } public static int func(){ int a = 10; try{ System.out.println("try中的代碼塊"); return a += 10; }catch (Exception e){ System.out.println("catch中的代碼塊"); }finally { System.out.println("finally中的代碼塊"); if(a > 10){ System.out.println("a>10,"+"a="+a); } return 100; } } }
運(yùn)行結(jié)果:
try中的代碼塊
finally中的代碼塊
a>10,a=20
100
注意:
(1)如果try中有return語句,finally中也有return語句,最終執(zhí)行的是finally中的return語句
(2)如果finally代碼塊中寫了return語句,那么finally之后的return語句就變成不可到達(dá)的語句,需要注釋掉,否則編譯不過
如果finally語句沒有return語句覆蓋返回值,那么原來的返回值可能因?yàn)閒inally里的修改而改變也有可能不變
(1)測試1
package exception; public class Demo08 { public static void main(String[] args) { System.out.println(func()); } public static int func(){ int a = 10; try{ System.out.println("try中的代碼塊"); return a += 20; }catch (Exception e){ e.printStackTrace(); System.out.println("catch中的代碼塊"); }finally { System.out.println("finally中的代碼塊"); a += 20; if(a > 10){ System.out.println("a > 10,a="+a); } a += 20; } return 200; } }
運(yùn)行結(jié)果:
try中的代碼塊
finally中的代碼塊
a > 10,a=50
30
注意:
對于基本數(shù)據(jù)類型來說,finally中對返回值的修改不會影響try中的返回變量的值
(2)測試2
package exception; import java.util.HashMap; import java.util.Map; public class Demo09 { 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<>(); map.put("KEY","INIT"); try{ map.put("KEY","try"); return map; }catch (Exception e){ e.printStackTrace(); map.put("KEY","catch"); }finally { map.put("KEY","finally"); map = null; } return map; } }
運(yùn)行結(jié)果:
finally
注意:
對于引用數(shù)據(jù)類型來說,finally中對返回值的修改會影響try中的返回變量的值
三、異常情況
try塊中的return語句在異常的情況下不會被執(zhí)行
package exception; public class Demo10 { public static void main(String[] args) { System.out.println(func()); } public static int func(){ int a = 10; try{ System.out.println("try中的代碼塊"); a = a/0; return a += 50; }catch (Exception e){ a += 15; System.out.println("catch中的代碼塊"); }finally { System.out.println("finally中的代碼塊"); if(a > 20){ System.out.println("a > 20,a ="+a); } a += 10; } return a; } }
運(yùn)行結(jié)果:
try中的代碼塊
catch中的代碼塊
finally中的代碼塊
a > 20,a =25
35
注意:
try語句塊中發(fā)生異常,try語句異常后的內(nèi)容不會執(zhí)行,return語句也不會執(zhí)行,執(zhí)行的是捕獲到的catch語句塊和finally語句塊
try中發(fā)生異常時(shí),return寫在catch語句中
package exception; public class Demo11 { public static void main(String[] args) { System.out.println(func()); } public static int func(){ int a = 10; try{ System.out.println("try中的代碼塊"); a = a /0; return a += 10; }catch (Exception e){ System.out.println("catch中的代碼塊"); return a += 15; }finally { System.out.println("finally中的代碼塊"); if (a > 10){ System.out.println("a > 10, a = "+a); } a += 50; System.out.println(a); } } }
運(yùn)行結(jié)果:
try中的代碼塊
catch中的代碼塊
finally中的代碼塊
a > 10, a = 25
75
25
注意:
try中發(fā)生異常之后,catch中的return語句先執(zhí)行,確定了返回值之后(保存起來,finally中的語句對返回值無影響)再去finally語句塊,執(zhí)行完之后再返回a的值,finally中對a的修改對返回值無效
四、finally語句一定會被執(zhí)行嗎?
(1)當(dāng)程序進(jìn)入try語句之前就出現(xiàn)異常時(shí),會直接結(jié)束
(2)try語句塊中有強(qiáng)制退出時(shí)也不會執(zhí)行finally語句塊中的代碼
System.exit(0);
代碼示例:
package exception; public class Demo12 { public static void main(String[] args) { int a = 10; try{ System.out.println("try block"); System.exit(0); }catch (Exception e){ System.out.println("catch block"); }finally { System.out.println("finally block"); } } }
運(yùn)行結(jié)果:
try block
到此這篇關(guān)于Java基礎(chǔ)之finally語句與return語句詳解的文章就介紹到這了,更多相關(guān)java finally語句與return語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot用多線程批量導(dǎo)入數(shù)據(jù)庫實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot用多線程批量導(dǎo)入數(shù)據(jù)庫實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02java實(shí)現(xiàn)計(jì)算周期性提醒的示例
本文分享一個(gè)java實(shí)現(xiàn)計(jì)算周期性提醒的示例,可以計(jì)算父親節(jié)、母親節(jié)這樣的節(jié)日,也可以定義如每月最好一個(gè)周五,以方便安排會議2014-04-04java實(shí)現(xiàn)IP地址轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)IP地址轉(zhuǎn)換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11SpringBoot+Spring?Data?JPA整合H2數(shù)據(jù)庫的示例代碼
H2數(shù)據(jù)庫是一個(gè)開源的關(guān)系型數(shù)據(jù)庫,本文重點(diǎn)給大家介紹SpringBoot+Spring?Data?JPA整合H2數(shù)據(jù)庫的示例代碼,感興趣的朋友跟隨小編一起看看吧2022-02-02Java中HashMap和Hashtable的區(qū)別淺析
這篇文章主要介紹了Java中HashMap和Hashtable的區(qū)別淺析,本文總結(jié)了6條它們之間的不同之處,需要的朋友可以參考下2015-03-03java簡單實(shí)現(xiàn)用語音讀txt文檔方法總結(jié)
在本篇文章里小編給大家整理了關(guān)于java簡單實(shí)現(xiàn)用語音讀txt文檔的詳細(xì)方法總結(jié),有需要的朋友們參考下。2019-06-06