设计的一个自定义异常的封装

本文介绍了一个名为D1XN的系统中使用的异常处理机制,包括异常枚举、自定义异常类及运行时异常类。通过这些类,可以更细致地控制和响应系统运行过程中发生的各种错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.d1xn.core.exception;

/**
 * 异常枚举
 * 
 * @author gonghf
 * @version 1.0 Jan 5, 2009
 */
public final class D1xnErrorEnum {

	public final static D1xnError E1000 = new D1xnError("1000", "没有该异常");
	public final static D1xnError E2000 = new D1xnError("2000", "数据库连接出错");

	final static D1xnError getError(String errorCode) {
		try {
			return (D1xnError) new D1xnErrorEnum().getClass().getDeclaredField(
					"E" + errorCode).get(new D1xnErrorEnum());

		} catch (Exception e) {
			throw new D1xnRuntimeException(D1xnErrorEnum.E1000);
		}

	}

	final static class D1xnError {

		private static final long serialVersionUID = 217246126247731875L;

		private String code;
		private String message;
		private D1xnError(String code, String msg) {
			this.message="d1xn-" + code + ":" + msg;
		}

		public final String getCode() {
			return this.code;
		}
		
		public final String getMessage() {
			return this.message;
		}
		
	}

}

 package com.d1xn.core.exception;


/**
 * D1XN异常
 * 
 * @author gonghf
 * @version 1.0 Jan 5, 2009
 */
public final class D1xnException extends Exception {

	private static final long serialVersionUID = 1387623981549785491L;
	private String code;

	public D1xnException(D1xnErrorEnum.D1xnError error) {
		super(error.getMessage());
		this.code=error.getCode();
	}

	public D1xnException(String errorCode) {
		super(D1xnErrorEnum.getError(errorCode).getMessage());
		this.code=errorCode;

	}

	public final String getCode() {
		return this.code;
	}
}

 package com.d1xn.core.exception;

/**
 * D1XN运行时异常
 * 
 * @author gonghf
 * @version 1.0 Jan 5, 2009
 */
public final class D1xnRuntimeException extends RuntimeException {

	private static final long serialVersionUID = 2325188130006865682L;
	private String code;
	public D1xnRuntimeException(D1xnErrorEnum.D1xnError error) {
		super(error.getMessage());
		this.code=error.getCode();
	}

	public D1xnRuntimeException(String errorCode) {
		super(D1xnErrorEnum.getError(errorCode).getMessage());
		this.code=errorCode;
	}

	public final String getCode() {
		return this.code;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值