3 回答

TA貢獻1816條經驗 獲得超4個贊
這是擴展 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貢獻1906條經驗 獲得超3個贊
你可以覆蓋類getMessage的方法Exception
class CustomException extends java.lang.Exception{
@Override
public String getMessage(){
return super.getMessage() + "- My Message";
}
}

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