3 回答

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個贊
這是擴(kuò)展 RuntimeException 類的完整 CustomException 類。您可以定義代碼和消息。
public class CustomException extends RuntimeException {
private String code;
private String message;
public CustomException() {
super();
}
public CustomException(Exception e) {
super(e);
}
public CustomException(String code, String message, Exception e) {
super(message, e);
this.code = code;
}
}

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個贊
你可以覆蓋類getMessage的方法Exception
class CustomException extends java.lang.Exception{
@Override
public String getMessage(){
return super.getMessage() + "- My Message";
}
}

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個贊
在您的代碼中拋出異常時
try{
//Some code here
}catch(Exception e){
throw new CustomeException("Your text here")
}
當(dāng)您在其他地方捕獲 CustomeException 時,您可以對其進(jìn)行修改。
try{
//Some code here
}catch(CustomException e){
String message = e.getMessage();
//Do stuff with previous message
throw new Custom2Exception("Your next text here")
}
添加回答
舉報(bào)