用枚举来处理java自定义异常
在系统开发过程中,总少不免要自己处理一些异常信息,然后将异常信息变成友好的提示返回到客户端的这样一个过程,之前都是new一个自定义的异常,当然这个所谓的自定义异常也是继承RuntimeException的,但这样往往会造成异常信息说明不一致的情况,所以就想到了用枚举来解决的办法。
1、先创建一个接口,里面提供两个方法,一个是getErrorCode, 一个是getErrorMessage,如:
1 2 3 4 5 6 7 | public interface IErrorCode { public String getErrorCode(); public String getErrorMessage(); } |
2、创建一个枚举,实现IErrorCode里的方法
public enum SysErrorEnums implements IErrorCode {
</span><span style="color: #008000">/**</span><span style="color: #008000">参数为空</span><span style="color: #008000">*/</span><span style="color: #000000"></br> EMPTY_PARAME(</span>"A11002","参数为空"<span style="color: #000000">),</br> </span><span style="color: #008000">/**</span><span style="color: #008000">参数错误</span><span style="color: #008000">*/</span><span style="color: #000000"></br> ERROR_PARAME(</span>"A11002","参数错误"<span style="color: #000000">);</br></br> </span><span style="color: #0000ff">private</span><span style="color: #000000"> String errorCode;</br> </span><span style="color: #0000ff">private</span><span style="color: #000000"> String errorMessage;</br></br> </span><span style="color: #0000ff">private</span><span style="color: #000000"> SysErrorEnums(String errorCode, String errorMessage) {</br> </span><span style="color: #0000ff">this</span>.errorCode =<span style="color: #000000"> errorCode;</br> </span><span style="color: #0000ff">this</span>.errorMessage =<span style="color: #000000"> errorMessage;</br> }</br></br> </span><span style="color: #0000ff">public</span><span style="color: #000000"> String getErrorCode() {</br> </span><span style="color: #0000ff">return</span><span style="color: #000000"> errorCode;</br> }</br></br> </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setErrorCode(String errorCode) {</br> </span><span style="color: #0000ff">this</span>.errorCode =<span style="color: #000000"> errorCode;</br> }</br> </span><span style="color: #0000ff">public</span><span style="color: #000000"> String getErrorMessage() {</br> </span><span style="color: #0000ff">return</span><span style="color: #000000"> errorMessage;</br> }</br></br> </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setErrorMessage(String errorMessage) {</br> </span><span style="color: #0000ff">this</span>.errorMessage =<span style="color: #000000"> errorMessage;</br> }</br>
}
3、定义一个自定义的异常类
public class BusinessException extends RuntimeException {
</span><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">long</span> serialVersionUID = 1L<span style="color: #000000">;</br></br> </span><span style="color: #0000ff">private</span><span style="color: #000000"> IErrorCode iErrorCode;</br></br> </span><span style="color: #0000ff">private</span><span style="color: #000000"> String errorCode;</br> </span><span style="color: #0000ff">private</span><span style="color: #000000"> String errorMessage;</br> </span><span style="color: #0000ff">private</span> Map<String, Object><span style="color: #000000"> errorData;</br></br> </span><span style="color: #0000ff">public</span><span style="color: #000000"> BusinessException(IErrorCode iErrorCode) {</br> </span><span style="color: #0000ff">super</span><span style="color: #000000">();</br> </span><span style="color: #0000ff">this</span>.iErrorCode =<span style="color: #000000"> iErrorCode;</br> </span><span style="color: #0000ff">this</span>.errorCode =<span style="color: #000000"> iErrorCode.getErrorCode();</br> </span><span style="color: #0000ff">this</span>.errorMessage =<span style="color: #000000"> iErrorCode.getErrorMessage();</br> }</br></br> </span><span style="color: #008000">//</span><span style="color: #008000">其他get、set、构造方法</span>
}
4、代码中抛异常
if(true){
throw new BusinessException(SysErrorEnums.EMPTY_OBJ);
}
5、可以通过异常拦截器来拦截错误,获取错误后统一格式输出;
这样做的好处是可以高度统一所有异常返回的code及message, 如果需要更改提示信息或代号,只需更改SysErrorEnums即可,并且可以自行添加多个异常枚举文件来分别对应不同的模板异常信息。代码结构简单,清淅。
</div>