异常码设计

定义异常码接口

public interface ErrorCode
{
    String getErrorCode();
}

定义数据库异常码

public enum DatabaseErrorCode implements ErrorCode
{


    DB_AUTH_FAILED("10000000"),
    ;
    
    private final String errorCode;  
    
    private DatabaseErrorCode(String errorCode) 
    {  
        this.errorCode = errorCode;  
    }  
    
    @Override
    public String getErrorCode()
    {
        return errorCode;
    }

}

定义基本异常类

public class GeneralException extends RuntimeException
{
    private static final long serialVersionUID = -6281969649055825675L;

    private ErrorCode errorCode;

    private Object[] parameters = null;

    private Map<String, Object> properties = new HashMap<String, Object>();

    public GeneralException(ErrorCode errorCode)
    {
        this.errorCode = errorCode;
    }

    public GeneralException(ErrorCode errorCode, Throwable cause)
    {
        super(cause);
        this.errorCode = errorCode;
    }

    public GeneralException(ErrorCode errorCode, Object[] parameters, Throwable cause)
    {
        super(cause);
        this.errorCode = errorCode;
        this.parameters = parameters;
    }

    @Override
    public String getMessage()
    {
        StringBuffer sbuffer = new StringBuffer();
        
        if (errorCode == null || errorCode == SystemErrorCode.UNKNOW_ERROR)
        {
            if(this.getCause() != null && getCause().getMessage() != null)
            {
                sbuffer.append(getCause().getMessage());
                return sbuffer.toString();
            }
        }
        // 从资源文件中获取异常码描述
        sbuffer.append(I18nUtils.getMessage(getErrorCode().getErrorCode()));
        String message = sbuffer.toString();
        if (this.getParameters() != null)
        {
            message = MessageFormat.format(message, this.getParameters());
        }

        return message;
    }

    public static GeneralException wrap(ErrorCode errorCode, Object[] parameters, Throwable exception)
    {
        if (exception instanceof GeneralException)
        {
            GeneralException se = (GeneralException) exception;
            if (errorCode != null && errorCode != se.getErrorCode())
            {
                return new GeneralException(errorCode, parameters, exception);
            }
            return se;
        }
        else
        {
            return new GeneralException(errorCode == null ? SystemErrorCode.UNKNOW_ERROR : errorCode, 
                    parameters, exception);
        }
    }

    public ErrorCode getErrorCode()
    {
        return errorCode;
    }

    public void setErrorCode(ErrorCode errorCode)
    {
        this.errorCode = errorCode;
    }

    public Object[] getParameters()
    {
        return parameters;
    }

    public void setParameters(Object[] parameters)
    {
        this.parameters = parameters;
    }

    public Object getProperty(String key)
    {
        return this.properties.get(key);
    }

    public GeneralException setProperty(String key, Object value)
    {
        this.properties.put(key, value);
        return this;
    }
    
    public static void main(String[] args)
    {
        try
        {
            throw new IllegalArgumentException();
        }
        catch(Exception e)
        {
            throw GeneralException.wrap(DatabaseErrorCode.DB_AUTH_FAILED, 
                    new Object[]{"test", "jdbc:mysql://localhost:3306/test"}, e);
        }
    }
}

资源文件

10000000=数据库认证错误,请检查用户名({0})、密码、数据库链接({1})是否正确。

测试

Exception in thread "main" com.huaxin.start.exception.GeneralException: 数据库认证错误,请检查用户名(test)、密码、数据库链接(jdbc:mysql://localhost:3306/test)是否正确。
at com.huaxin.start.exception.GeneralException.wrap(GeneralException.java:89)
at com.huaxin.start.exception.GeneralException.main(GeneralException.java:133)
Caused by: java.lang.IllegalArgumentException
at com.huaxin.start.exception.GeneralException.main(GeneralException.java:129)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值