異常鏈的小問(wèn)題
為什么我public void test1() throws DrunkException{ ?這里的DrunkException提示DrunkException cannot be resolved to a type呢...
Java入門(mén)第三季1-7異常鏈的
代碼如下
package imoocChapterThree;
public class ChainTest {
/**
* test1():拋出異常
* test2():調(diào)用test1(),捕獲異常,并且包裝成運(yùn)行時(shí)異常,繼續(xù)拋出
* main方法中,調(diào)用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("喝車(chē)別開(kāi)酒!");
}
public void test2(){
try {
test1();
} catch (DrunkException e) {
// TODO: handle exception
RuntimeException newExc =
new RuntimeException("司機(jī)一滴酒,親人兩行淚");
newExc.initCause(e);
throw newExc;
}
}
}
2017-01-05
可能是你DrunkException這個(gè)異常寫(xiě)錯(cuò)了,你有沒(méi)有繼承Exception?