国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Java基礎(chǔ)之finally語句與return語句詳解

瀏覽:4日期:2022-08-13 14:52:53
一、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=2020

注意:

a > 10,a=20的結(jié)果說明了return a += 10已經(jīng)執(zhí)行了,但是沒有直接返回,而是先去執(zhí)行finally語句的內(nèi)容,然后再去返回結(jié)果

二、覆蓋問題

finally塊中的return語句會(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;} }}

運(yùn)行結(jié)果:

try中的代碼塊finally中的代碼塊a>10,a=20100

注意:

(1)如果try中有return語句,finally中也有return語句,最終執(zhí)行的是finally中的return語句(2)如果finally代碼塊中寫了return語句,那么finally之后的return語句就變成不可到達(dá)的語句,需要注釋掉,否則編譯不過

如果finally語句沒有return語句覆蓋返回值,那么原來的返回值可能因?yàn)閒inally里的修改而改變也有可能不變

(1)測(cè)試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=5030

注意:

對(duì)于基本數(shù)據(jù)類型來說,finally中對(duì)返回值的修改不會(huì)影響try中的返回變量的值

(2)測(cè)試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

注意:

對(duì)于引用數(shù)據(jù)類型來說,finally中對(duì)返回值的修改會(huì)影響try中的返回變量的值

三、異常情況

try塊中的return語句在異常的情況下不會(huì)被執(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 =2535

注意:

try語句塊中發(fā)生異常,try語句異常后的內(nèi)容不會(huì)執(zhí)行,return語句也不會(huì)執(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 = 257525

注意:

try中發(fā)生異常之后,catch中的return語句先執(zhí)行,確定了返回值之后(保存起來,finally中的語句對(duì)返回值無影響)再去finally語句塊,執(zhí)行完之后再返回a的值,finally中對(duì)a的修改對(duì)返回值無效

四、finally語句一定會(huì)被執(zhí)行嗎?

(1)當(dāng)程序進(jìn)入try語句之前就出現(xiàn)異常時(shí),會(huì)直接結(jié)束

(2)try語句塊中有強(qiáng)制退出時(shí)也不會(huì)執(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)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 峡江县| 息烽县| 成都市| 桃园市| 洪雅县| 邹平县| 洛扎县| 澄江县| 新津县| 邯郸县| 卢氏县| 日照市| 大兴区| 奈曼旗| 广州市| 阿合奇县| 遵义县| 莫力| 卢龙县| 武功县| 比如县| 绥棱县| 阳谷县| 东阳市| 肇源县| 时尚| 尼勒克县| 天全县| 松滋市| 呼图壁县| 扬中市| 龙川县| 周宁县| 大理市| 平潭县| 大悟县| 桂东县| 大悟县| 桃源县| 宁化县| 抚宁县|