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

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

為什么我代碼的不輸出test1的內容“喝車不開酒!”??

public class ChainTest {

/**

* test1():拋出"喝大了"異常

* test2():調用test1(),捕獲"喝大了"異常,并且包裝成運行時異常,繼續拋出

* main方法中,調用test2(),嘗試捕獲test2()方法跑出的異常

*/

public static void main(String[] args) {

ChainTest ct = new ChainTest();

try{

ct.test2();

}catch(Exception e){

e.printStackTrace();

}

}

public void test1() throws DrunkException{

throw new DrunkException("喝車不開酒!");

}


public void test2(){

try {

test1();

} catch (DrunkException e) {

// TODO Auto-generated catch block

RuntimeException newExc = new RuntimeException("司機一滴酒,親人兩行淚~~");

newExc.initCause(e);

throw newExc;

}

}

}


正在回答

1 回答

我復制了你的程序然后按照老師的寫個DrunkException后

是可以輸出錯誤的

結果如下:

com.scu.DrunkException: 喝車不開酒!

at com.scu.ChainTest.test1(ChainTest.java:19)

at com.scu.ChainTest.test2(ChainTest.java:24)

at com.scu.ChainTest.main(ChainTest.java:12)

java.lang.RuntimeException: 司機一滴酒,親人兩行淚~~

at com.scu.ChainTest.test2(ChainTest.java:28)

at com.scu.ChainTest.main(ChainTest.java:12)

Caused by: com.scu.DrunkException: 喝車不開酒!

at com.scu.ChainTest.test1(ChainTest.java:19)

at com.scu.ChainTest.test2(ChainTest.java:24)

... 1 more

可能是軟件的問題,重啟一下可能就好了

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

傳說中的高手

對了我在test2()的catch里也加了一個printStackTrace()
2018-01-07 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么我代碼的不輸出test1的內容“喝車不開酒!”??

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

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

幫助反饋 APP下載

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

公眾號

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