寫DrunkException的時(shí)候出現(xiàn)了一個(gè)問題:類型 serializable 類 DrunkException 未聲明類型為 long 的靜態(tài)終態(tài) serialVersionUID 字段
這個(gè)代碼有錯(cuò):
package imooc.exception.test;
public class DrunkException extends Exception {
public DrunkException() {}
public DrunkException(String message) {
super(message);
}
}
那個(gè)類那邊提示類型 serializable 類 DrunkException 未聲明類型為 long 的靜態(tài)終態(tài) serialVersionUID 字段,這是什么意思呀?
下面是ChainTest類,看看有沒有錯(cuò):
package imooc.exception.test;
public class ChainTest {
/*
* 這個(gè)類完成的工作:第一個(gè)方法(Test1):拋出“喝大了”異常;
* 第二個(gè)方法(Test2):調(diào)用Test1,捕獲“喝大了”異常,并包裝成運(yùn)行時(shí)異常,繼續(xù)拋出;
* main方法中調(diào)用test2,嘗試捕獲test2方法拋出的異常
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ChainTest ct = new ChainTest();
try {
ct.test2();
}catch(Exception e) {
e.getStackTrace();
}
}
public void test1() throws DrunkException {
throw new DrunkException("喝車別開酒!");
}
public void test2() {?
try {
test1();
} catch (DrunkException e) {
// TODO 自動(dòng)生成的 catch 塊
//e.printStackTrace();這個(gè)就不需要了
RuntimeException newExc = new RuntimeException("司機(jī)一滴酒,親人兩行淚");//新建一個(gè)RuntimeException,起名叫newExc
newExc.initCause(e);//調(diào)用newExc的initCause方法并把捕獲的DrunkException放進(jìn)去
throw newExc;//拋出新異常
}
}
}
2019-08-27