異常鏈的小問題
為什么我public void test1() throws DrunkException{ ?這里的DrunkException提示DrunkException cannot be resolved to a type呢...
Java入門第三季1-7異常鏈的
代碼如下
package imoocChapterThree;
public class ChainTest {
/**
* test1():拋出異常
* test2():調用test1(),捕獲異常,并且包裝成運行時異常,繼續拋出
* main方法中,調用test2(),嘗試捕獲test2()方法拋出的異常
* @param args
*/
public static void main(String[] args) {
ChainTest hello = new ChainTest();
try {
hello.test2();
} catch (Exception e) {
e.printStackTrace();
}
}
public void test1() throws DrunkException{
/*申明將要拋出這種拋出異常*/
throw new DrunkException("喝車別開酒!");
}
public void test2(){
try {
test1();
} catch (DrunkException e) {
// TODO: handle exception
RuntimeException newExc =
new RuntimeException("司機一滴酒,親人兩行淚");
newExc.initCause(e);
throw newExc;
}
}
}
2017-01-05
可能是你DrunkException這個異常寫錯了,你有沒有繼承Exception?