Exception异常类

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();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值