亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

網上看到這段代碼,沒有像課里講的一樣產生異常鏈?

public?class?TestException?{??

????public?TestException()?{??

????}??

??

????boolean?testEx()?throws?Exception?{??

????????boolean?ret?=?true;??

????????try?{??

????????????ret?=?testEx1();??

????????}?catch?(Exception?e)?{??

????????????System.out.println("testEx,?catch?exception");??

????????????ret?=?false;??

????????????throw?e;??

????????}?finally?{??

????????????System.out.println("testEx,?finally;?return?value="?+?ret);??

????????????return?ret;??

????????}??

????}??

??

????boolean?testEx1()?throws?Exception?{??

????????boolean?ret?=?true;??

????????try?{??

????????????ret?=?testEx2();??

????????????if?(!ret)?{??

????????????????return?false;??

????????????}??

????????????System.out.println("testEx1,?at?the?end?of?try");??

????????????return?ret;??

????????}?catch?(Exception?e)?{??

????????????System.out.println("testEx1,?catch?exception");??

????????????ret?=?false;??

????????????throw?e;??

????????}?finally?{??

????????????System.out.println("testEx1,?finally;?return?value="?+?ret);??

????????????return?ret;??

????????}??

????}??

??

????boolean?testEx2()?throws?Exception?{??

????????boolean?ret?=?true;??

????????try?{??

????????????int?b?=?12;??

????????????int?c;??

????????????for?(int?i?=?2;?i?>=?-2;?i--)?{??

????????????????c?=?b?/?i;??

????????????????System.out.println("i="?+?i);??

????????????}??

????????????return?true;??

????????}?catch?(Exception?e)?{??

????????????System.out.println("testEx2,?catch?exception");??

????????????ret?=?false;??

????????????throw?e;??

????????}?finally?{??

????????????System.out.println("testEx2,?finally;?return?value="?+?ret);??

????????????return?ret;??

????????}??

????}??

??

????public?static?void?main(String[]?args)?{??

????????TestException?testException1?=?new?TestException();??

????????try?{??

????????????testException1.testEx();??

????????}?catch?(Exception?e)?{??

????????????e.printStackTrace();??

????????}??

????}??

} ?

代碼輸出結果是:

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

?不明白的是為什么testEx1和testEx都沒有捕獲到異常。

正在回答

3 回答

你throw了一個異常,然后finally中又返回了一個值,相當于方法正常結束,所以異常就沒了。finally中不要加return。

1 回復 有任何疑惑可以回復我~
#1

云與米修 提問者

原來是這么回事,明白了,謝謝。
2016-04-08 回復 有任何疑惑可以回復我~
#2

我真的不知道要取什么名字

是不是說,我已經用這個聲明的異常加finally已經解決了代碼中的問題,讓程序正常運行,就不出現異常。
2016-05-31 回復 有任何疑惑可以回復我~

boolean?ret?=?true;??

????????try?{??

????????????ret?=?testEx2();??

????????????if?(!ret)?{??

????????????????return?false;??

????????????}??

????????????System.out.println("testEx1,?at?the?end?of?try");??

????????????return?ret; ?

這段代碼的意思是定義ret為真,如果testEx2(); 返回值為假,則拋出異常

0 回復 有任何疑惑可以回復我~

這個程序的輸出順序是先testEx2,再testEx,最后testEx。而為什么視頻上程序的輸出順序是先test2,再text1?和什么有關

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

網上看到這段代碼,沒有像課里講的一樣產生異常鏈?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號