- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- Java基礎之finally語(yǔ)句與return語(yǔ)句詳解
finally語(yǔ)句是在return語(yǔ)句執行之后,return語(yǔ)句返回之前執行的
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; } }
運行結果:
try中的代碼塊
finally中的代碼塊
a > 10,a=20
20
注意:
a > 10,a=20的結果說(shuō)明了return a += 10已經(jīng)執行了,但是沒(méi)有直接返回,而是先去執行finally語(yǔ)句的內容,然后再去返回結果
finally塊中的return語(yǔ)句會(huì )覆蓋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; } } }
運行結果:
try中的代碼塊
finally中的代碼塊
a>10,a=20
100
注意:
(1)如果try中有return語(yǔ)句,finally中也有return語(yǔ)句,最終執行的是finally中的return語(yǔ)句
(2)如果finally代碼塊中寫(xiě)了return語(yǔ)句,那么finally之后的return語(yǔ)句就變成不可到達的語(yǔ)句,需要注釋掉,否則編譯不過(guò)
如果finally語(yǔ)句沒(méi)有return語(yǔ)句覆蓋返回值,那么原來(lái)的返回值可能因為finally里的修改而改變也有可能不變
(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; } }
運行結果:
try中的代碼塊
finally中的代碼塊
a > 10,a=50
30
注意:
對于基本數據類(lèi)型來(lái)說(shuō),finally中對返回值的修改不會(huì )影響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; } }
運行結果:
finally
注意:
對于引用數據類(lèi)型來(lái)說(shuō),finally中對返回值的修改會(huì )影響try中的返回變量的值
try塊中的return語(yǔ)句在異常的情況下不會(huì )被執行
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; } }
運行結果:
try中的代碼塊
catch中的代碼塊
finally中的代碼塊
a > 20,a =25
35
注意:
try語(yǔ)句塊中發(fā)生異常,try語(yǔ)句異常后的內容不會(huì )執行,return語(yǔ)句也不會(huì )執行,執行的是捕獲到的catch語(yǔ)句塊和finally語(yǔ)句塊
try中發(fā)生異常時(shí),return寫(xiě)在catch語(yǔ)句中
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); } } }
運行結果:
try中的代碼塊
catch中的代碼塊
finally中的代碼塊
a > 10, a = 25
75
25
注意:
try中發(fā)生異常之后,catch中的return語(yǔ)句先執行,確定了返回值之后(保存起來(lái),finally中的語(yǔ)句對返回值無(wú)影響)再去finally語(yǔ)句塊,執行完之后再返回a的值,finally中對a的修改對返回值無(wú)效
(1)當程序進(jìn)入try語(yǔ)句之前就出現異常時(shí),會(huì )直接結束
(2)try語(yǔ)句塊中有強制退出時(shí)也不會(huì )執行finally語(yǔ)句塊中的代碼
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"); } } }
運行結果:
try block
到此這篇關(guān)于Java基礎之finally語(yǔ)句與return語(yǔ)句詳解的文章就介紹到這了,更多相關(guān)java finally語(yǔ)句與return語(yǔ)句內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站