/**
* 自定义异常类
*/
public class ServiceExpection extends RuntimeException{
private Integer code;
private String message;
public ServiceExpection() {
}
public ServiceExpection(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
使用
try {
throw new ServiceExpection(106,"发生了自定义错误");
}catch (ServiceExpection expection){
System.out.println("捕获了异常:"+expection.getMessage());
}