定义异常码接口
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)