package exception;
/**
* 自定义异常
* @author issuser
*
*/
public class McoreException extends Exception {
private static final long serialVersionUID = -2092838637923256699L;
/**
* 错误信息
*/
private String error ;
/**
* 错误编码
*/
private String code ;
/**
* 错误枚举
*/
private ErrorCode ec;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public ErrorCode getErrorCode(){
return this.ec;
}
public McoreException(ErrorCode ec){
this(ec.getError(),ec.getCode());
this.ec = ec;
this.error = ec.getError();
this.code = ec.getCode();
}
public McoreException(String error,String code){
super(error);
if(this.ec == null){
this.ec = ErrorCode.get(code);
}
if(this.ec != null){
this.ec.setError(error);
}
}
public McoreException(){
super();
}
public McoreException(String error){
super(error);
}
public McoreException(Throwable agr0){
super(agr0);
}
/**
*
* @param msg
* @param agr0
* @param isp 是否启用堆栈跟踪
*/
public McoreException(String msg,Throwable agr0,Boolean isp){
super(msg, agr0, true, isp);
}
/**
* enableSuppression 是否启用抑制异常,如禁止,则getSuppressed方法返回长度为0的数组
* writableStackTrace 是否启用堆栈跟踪
*/
public McoreException(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public static void main(String[] args) throws Exception {
// throw new McoreException(new McoreException("11"));
// throw new McoreException("12",new McoreException("11"),true,true);
throw new McoreException(ErrorCode.error001);
}
}
enum ErrorCode{
error001("错误1"),
error002("错误2"),;
private String error;
private ErrorCode(String error) {
this.error = error;
}
public boolean is(ErrorCode ec){
return name().equals(ec.getCode());
}
/**
* 获取枚举类
* @param code
* @return
*/
public static ErrorCode get(String code){
for(ErrorCode ec : values()){
if(ec.name().equals(code)){
return ec;
}
}
return null;
}
public String getError(){
return this.error;
}
public void setError(String error){
this.error = error;
}
public String getCode(){
return this.name();
}
}