问答详情
源自:1-7 Java 中的异常链

控制台无输出

package test.exception;


public class ChainTest {

/*

* test1():抛出"喝大了"异常 test2():调用test1(),捕获"喝大了"异常,并且包装成 "运行时异常",继续抛出

* main方法中,调用test2(),尝试捕获test2()方法抛出的异常

*/

public void test1() throws DrunkException {

new DrunkException("喝车别开酒!");

}


public void test2() {

try {

test1();

} catch (DrunkException e) {

// TODO Auto-generated catch block

RuntimeException newExc = new RuntimeException("司机一滴酒 亲人两行泪~~");

newExc.initCause(e);

throw newExc;

}

}


public static void main(String[] args) {

ChainTest ct = new ChainTest();

try {

ct.test2();

} catch (Exception e) {

e.printStackTrace();

}

}

}


提问者:慕前端9263910 2020-02-19 19:19

个回答

  • 慕瓜7049329
    2020-02-19 23:35:24

    public void test1() throws DrunkException {

    new DrunkException("喝车别开酒!");

    }

    少了 throw