异常抛出类

package com.lz.ctsframework.core.support;


import java.text.MessageFormat;


/**
 * 
 * <b>类说明:</b>Service层统一抛出的异常
 * 
 * <p>
 * <b>详细描述:</b>
 * 
 * @author liuhuanchao
 * @since 2016-04-18
 */
public class ServiceException extends RuntimeException {	


    private static final long serialVersionUID = 6514891174875747380L;




    /** 异常错误码 **/
    private String code;


    /** 异常描述 **/
    private String msg; 
    /** 扩展异常描述(包括msg) **/
    private String extMsg;


    /**
     * ServiceException构造方法,有format字符组
     * @param errorCode 错误码
     * @param param     format字符组
     * @param extMsg    扩展信息,给出具体的错误值信息等
     */
    public ServiceException(ErrorCode errorCode,String param[],String ... extMsg) {
        super(null==errorCode ? "" : errorCode.getCode());
        init(errorCode, param,extMsg);
    }


    /**
     * ServiceException构造方法,有format字符组
     * @param errCode
     * @param paramsList
     */
    public ServiceException(ErrorCode errCode, Object... paramsList) {
        Object[] params = null;
        if ((paramsList != null) && (paramsList.length > 0) 
                && ((paramsList[(paramsList.length - 1)] instanceof Throwable))) 
        {
            Object[] newParam = new Object[paramsList.length - 1];
            System.arraycopy(paramsList, 0, newParam, 0, newParam.length);
            params = newParam;
            super.initCause((Throwable)paramsList[(paramsList.length - 1)]);
        }
        else {
            params = paramsList;
            super.initCause(null);
        }


        this.code = null==errCode ? "" : errCode.getCode();
        this.msg = null==errCode ? "" :  MessageFormat.format(errCode.getMsg(),params);  
        this.extMsg = this.msg;
    }


    private void init(ErrorCode errorCode, String param[], String... extMsg) {
        this.code = null==errorCode ? "" : errorCode.getCode();
        this.msg = null==errorCode ? "" : MessageFormat.format(errorCode.getMsg(),param);
        StringBuilder builder = new StringBuilder(100);
        builder.append(this.msg);
        if(null != extMsg){
            for(String ext : extMsg ){
                builder.append("[").append(ext).append("]");
            }
        }
        this.extMsg = builder.toString();
    }


    /**
     * 
     * @param code  错误码
     * @param msg 描述信息
     */
    public ServiceException(String code, String msg) {
        super(code+":"+msg);
        this.code = code;
        this.msg = msg;
    }


    /**
     * 带Exception的构造方法,传format字符数组
     * @param errorCode 错误码基类
     * @param e  异常
     * @param extMsg    扩展信息,给出具体的错误值信息等
     */
    public ServiceException(ErrorCode errorCode, Throwable e,String param[] , String ...extMsg ) {
        super(null==errorCode ? "" : errorCode.getCode(), e);
        init(errorCode, param, extMsg);
    }


    /**
     * 
     * @param code 错误码
     * @param msg 描述信息
     * @param e  异常
     */
    /*public ServiceException(String code, String msg,Throwable e) {
		super(code+":"+msg, e);
		this.code = code;
		this.msg = msg; 
	}*/


    /**
     * 
     * 
     * 方法说明:异常错误码
     * 
     * @return
     */
    public String getCode() {
        return code;
    }


    /**
     * 
     * 
     * 方法说明:异常描述信息
     * 
     * @return
     */
    public String getMsg() {
        return msg;
    }


    public String getExtMsg() {
        return extMsg;
    }


    @Override
    public String getMessage() {		
        return super.getMessage() + ","+extMsg;
    }	


    public static void main(String[] args) {


    }


}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值