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;
}
}
}
我复制了你的程序然后按照老师的写个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
可能是软件的问题,重启一下可能就好了