java统一异常处理总结

8 篇文章 1 订阅
异常处理机制

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

}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值