枚举类的使用(固定信息)+ 自定义异常类Exception

枚举类的使用(固定信息)

使用

IbosOrderCenterError.PARAMETER_CANNOT_BE_NULL

或者

IbosOrderCenterError.PARAMETER_CANNOT_BE_NULL.getErrorMsg()

/**
 * @author Sun
 */
public enum IbosOrderCenterError {

	/** 未指明的异常 */
	UNKNOW("000", "未知异常"),
	/** 参数不能为空 */
	PARAMETER_CANNOT_BE_NULL("001", "参数不能为空"),
	/**购物车订单新*/
	CALL_ORDER_FORCED_CANCEL_ERROR("002", "购物更新 json_newbuy_orderCenter_orderForcedCancel接口异常");


	/** 错误码 */
	private final String errorCode;

	/** 错误信息 */
	private final String errorMsg;

	/** 构造函数 */
	private IbosOrderCenterError(String errorCode, String errorMsg) {
		this.errorCode = errorCode;
		this.errorMsg = errorMsg;
	}

	public static IbosOrderCenterError getMsg(String errorCode) {
		for (IbosOrderCenterError value : IbosOrderCenterError.values()) {
			if (errorCode.equals(value.getErrorCode())) {
				return value;
			}
		}
		return UNKNOW;
	}

	public String getErrorCode() {
		return errorCode;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

}

自定义异常类Exception

/**
 * 业务异常,所有异常需定义在IBOSBusinessError枚举类中
 * @author Sun
 * */
public class IbosOrderCenterException extends RuntimeException {

	/**
	 *
	 */
	private static final long serialVersionUID = 1896596275901239775L;

	private final String errorCode;

	private final String errorMsg;

	/** 直接抛出未知异常,不建议使用*/
	public IbosOrderCenterException() {
		super(IbosOrderCenterError.UNKNOW.getErrorMsg());
		this.errorCode = IbosOrderCenterError.UNKNOW.getErrorCode();
		this.errorMsg = IbosOrderCenterError.UNKNOW.getErrorMsg();
	}

	/** 抛出枚举类中异常,推荐使用*/
	public IbosOrderCenterException(IbosOrderCenterError error) {
		super(error.getErrorMsg());
		this.errorCode = error.getErrorCode();
		this.errorMsg = error.getErrorMsg();
	}

	/** 抛出枚举类中异常,推荐使用
	 * @param  error 枚举类中定义
	 * @param  detailMsg 异常详细描述
	 * */
	public IbosOrderCenterException(IbosOrderCenterError error, String detailMsg) {
		super(error.getErrorMsg()+","+"接口返回:["+detailMsg+"]");
		this.errorCode = error.getErrorCode();
		this.errorMsg = error.getErrorMsg()+","+"接口返回:["+detailMsg+"]";
	}

	/** 抛出枚举类中异常,推荐使用
	 * @param  error 枚举类中定义
	 * @param  detailMsg 异常详细描述
	 * @param  transId 流水
	 * */
	public IbosOrderCenterException(IbosOrderCenterError error, String detailMsg, String transId) {
		super(error.getErrorMsg()+","+"接口返回:["+detailMsg+"]"+"
"
+"transId:"+transId); this.errorCode = error.getErrorCode(); this.errorMsg = error.getErrorMsg()+","+"接口返回:["+detailMsg+"]"+"
"
+"transId:"+transId; } public String getErrorCode() { return errorCode; } public String getErrorMsg() { return errorMsg; } }

你可能感兴趣的:(Java开发,java,开发语言)