您的位置:首页 > 其它

异常抛出类

2016-04-18 14:36 274 查看
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) {

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: