异常处理机制
exception包:
AlarmRuntimeException.java
BusinessRuntimeException.java
ExceptionBean.java
3.ExceptionBean:Exception的javaBean,code和msg
4.ExceptionEnum:异常枚举类,使用了MessageFormat格式化msg。主要是addPams写入到格式化的字符串中。
调用方式:
BusinessRuntimeException
ExceptionBean
ExceptionEnum
exception包:
AlarmRuntimeException.java
BusinessRuntimeException.java
ExceptionBean.java
ExceptionEnum.java
1.AlarmRuntimeException:报警时候使用,因为AlarmRuntimeException继承了RuntimeException,相当于抛出RuntimeException。没有做其他处理
2.BusinessRuntimeException:相当于封装了运行时异常,字符串 code:''msg:'',还有Throwable抛出的异常,还有自定义的ExceptionBean和ExceptionEnum
public BusinessRuntimeException(String code, String msg) {
super("code:".concat(code).concat("msg:").concat(msg));
this.code = code;
this.msg = msg;
}
public BusinessRuntimeException(String code, String msg, Throwable t) {
super("code:".concat(code).concat("msg:").concat(msg), t);
this.code = code;
this.msg = msg;
}
public BusinessRuntimeException(ExceptionBean exceptionBean) {
super("code:".concat(exceptionBean.getCode()).concat("msg:").concat(exceptionBean.getMsg()));
this.code = exceptionBean.getCode();
this.msg = exceptionBean.getMsg();
}
public BusinessRuntimeException(ExceptionBean exceptionBean, Throwable t) {
super("code:".concat(exceptionBean.getCode()).concat("msg:").concat(exceptionBean.getMsg()),t);
this.code = exceptionBean.getCode();
this.msg = exceptionBean.getMsg();
}
public BusinessRuntimeException(ExceptionEnum exceptionEnum) {
super("code:".concat(exceptionEnum.getCode()).concat("msg:").concat(exceptionEnum.getMsg()));
this.code = exceptionEnum.getCode();
this.msg = exceptionEnum.getMsg();
}
public BusinessRuntimeException(ExceptionEnum exceptionEnum, Throwable t) {
super("code:".concat(exceptionEnum.getCode()).concat("msg:").concat(exceptionEnum.getMsg()),t);
this.code = exceptionEnum.getCode();
this.msg = exceptionEnum.getMsg();
}
3.ExceptionBean:Exception的javaBean,code和msg
4.ExceptionEnum:异常枚举类,使用了MessageFormat格式化msg。主要是addPams写入到格式化的字符串中。
SYSTEM_ERROR("001","system error please try again"),
PARAM_EMPTY("002","param {0} must not null or empty"),
PARAM_ERROR("003","param {0} must useful"),
SERIALRETURN_ERROR("004","get serial return error param {0}"),
REMOTE_CALL_ERROR("005","call remote interface {0} method {1} exception param {2}"),
public ExceptionBean addPams(Object... objs) {
if (objs == null || objs.length <= 0) {
return new ExceptionBean(getCode(), EMPTY);
}
return new ExceptionBean(getCode(), MessageFormat.format(getMsg(), objs));
}
public ExceptionBean addPams(Collection<?> coll) {
if (coll == null || coll.size() <= 0) {
return new ExceptionBean(getCode(), EMPTY);
}
return addPams(coll.toArray());
}
public ExceptionBean appendMsg(String app) {
return new ExceptionBean(getCode(), getMsg().concat(app));
}
@Override
public String toString() {
return "code:".concat(getCode()).concat("msg:").concat(getMsg());
}
调用方式:
throw new BusinessRuntimeException(ExceptionEnum.PARAM_ERROR.addPams("billNo or tradeNo"));
throw new BusinessRuntimeException(ExceptionEnum.REMOTE_CALL_ERROR.addPams("ServiceExport", "Rollback", "billNo:".concat(billNo)));
AlarmRuntimeException
public class AlarmRuntimeException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = -3208398073746622202L;
private String msg;
public AlarmRuntimeException(String msg) {
super(msg);
this.msg = msg;
}
public AlarmRuntimeException(String msg,Throwable t) {
super(msg,t);
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
BusinessRuntimeException
public class BusinessRuntimeException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 439936386847345501L;
private String code;
private String msg;
public BusinessRuntimeException(String code, String msg) {
super("code:".concat(code).concat("msg:").concat(msg));
this.code = code;
this.msg = msg;
}
public BusinessRuntimeException(String code, String msg, Throwable t) {
super("code:".concat(code).concat("msg:").concat(msg), t);
this.code = code;
this.msg = msg;
}
public BusinessRuntimeException(ExceptionBean exceptionBean) {
super("code:".concat(exceptionBean.getCode()).concat("msg:").concat(exceptionBean.getMsg()));
this.code = exceptionBean.getCode();
this.msg = exceptionBean.getMsg();
}
public BusinessRuntimeException(ExceptionBean exceptionBean, Throwable t) {
super("code:".concat(exceptionBean.getCode()).concat("msg:").concat(exceptionBean.getMsg()),t);
this.code = exceptionBean.getCode();
this.msg = exceptionBean.getMsg();
}
public BusinessRuntimeException(ExceptionEnum exceptionEnum) {
super("code:".concat(exceptionEnum.getCode()).concat("msg:").concat(exceptionEnum.getMsg()));
this.code = exceptionEnum.getCode();
this.msg = exceptionEnum.getMsg();
}
public BusinessRuntimeException(ExceptionEnum exceptionEnum, Throwable t) {
super("code:".concat(exceptionEnum.getCode()).concat("msg:").concat(exceptionEnum.getMsg()),t);
this.code = exceptionEnum.getCode();
this.msg = exceptionEnum.getMsg();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
ExceptionBean
import java.io.Serializable;
public class ExceptionBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3612529074138518136L;
private String code;
private String msg;
public ExceptionBean() {
}
public ExceptionBean(String code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public String toString() {
return "code:".concat(code).concat("msg:").concat(msg);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
ExceptionEnum
import java.text.MessageFormat;
import java.util.Collection;
public enum ExceptionEnum {
SYSTEM_ERROR("001","system error please try again"),
PARAM_EMPTY("002","param {0} must not null or empty"),
PARAM_ERROR("003","param {0} must useful"),
SERIALRETURN_ERROR("004","get serial return error param {0}"),
REMOTE_CALL_ERROR("005","call remote interface {0} method {1} exception param {2}"),
LOCK_ERROR("008","lock error type {0} key {1}"),
;
private static final String EMPTY = "";
private String code;
private String msg;
private ExceptionEnum(String code, String msg) {
this.code = code;
this.msg = msg;
}
public ExceptionBean addPams(Object... objs) {
if (objs == null || objs.length <= 0) {
return new ExceptionBean(getCode(), EMPTY);
}
return new ExceptionBean(getCode(), MessageFormat.format(getMsg(), objs));
}
public ExceptionBean addPams(Collection<?> coll) {
if (coll == null || coll.size() <= 0) {
return new ExceptionBean(getCode(), EMPTY);
}
return addPams(coll.toArray());
}
public ExceptionBean appendMsg(String app) {
return new ExceptionBean(getCode(), getMsg().concat(app));
}
@Override
public String toString() {
return "code:".concat(getCode()).concat("msg:").concat(getMsg());
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}