哪位大神给自己公司开发过好的异常处理机制,求指点一个好的方案

我公司现在需要开发一套自己的异常处理机制,就是当异常出现时,在客户端能够返回具体是哪的什么异常,如微信API以及一些大公司做的那种,求指点方案。

定义一套有规律异常编码,一看返回编码就知道哪里有异常

主要就是定义错误码,可以参考微软的GetLastError的做法,就是提供了各种各样的错误码信息,这样用户可以查询了解具体的错误原因。

package com.lz.ctsframework.core.support;

import java.text.MessageFormat;

/**

  • 类说明:Service层统一抛出的异常
  • 详细描述:
  • */
    public class ServiceException extends RuntimeException {

    private static final long serialVersionUID = 6514891174875747380L;

    /** 异常错误码 **/
    private String code;

    /** 异常描述 /
    private String msg;
    /
    扩展异常描述(包括msg) **/
    private String extMsg;

    /**

    • ServiceException构造方法,有format字符组
    • @param errorCode 错误码
    • @param param format字符组
    • @param extMsg 扩展信息,给出具体的错误值信息等 */ public ServiceException(ErrorCode errorCode,String param[],String ... extMsg) { super(null==errorCode ? "" : errorCode.getCode()); init(errorCode, param,extMsg); }

    /**

    • ServiceException构造方法,有format字符组
    • @param errCode
    • @param paramsList
      */
      public ServiceException(ErrorCode errCode, Object... paramsList) {
      Object[] params = null;
      if ((paramsList != null) && (paramsList.length > 0)
      && ((paramsList[(paramsList.length - 1)] instanceof Throwable)))
      {
      Object[] newParam = new Object[paramsList.length - 1];
      System.arraycopy(paramsList, 0, newParam, 0, newParam.length);
      params = newParam;
      super.initCause((Throwable)paramsList[(paramsList.length - 1)]);
      }
      else {
      params = paramsList;
      super.initCause(null);
      }

      this.code = null==errCode ? "" : errCode.getCode();
      this.msg = null==errCode ? "" : MessageFormat.format(errCode.getMsg(),params);

      this.extMsg = this.msg;
      }

    private void init(ErrorCode errorCode, String param[], String... extMsg) {
    this.code = null==errorCode ? "" : errorCode.getCode();
    this.msg = null==errorCode ? "" : MessageFormat.format(errorCode.getMsg(),param);
    StringBuilder builder = new StringBuilder(100);
    builder.append(this.msg);
    if(null != extMsg){
    for(String ext : extMsg ){
    builder.append("[").append(ext).append("]");
    }
    }
    this.extMsg = builder.toString();
    }

    /**

    • @param code 错误码
    • @param msg 描述信息 */ public ServiceException(String code, String msg) { super(code+":"+msg); this.code = code; this.msg = msg; }

    /**

    • 带Exception的构造方法,传format字符数组
    • @param errorCode 错误码基类
    • @param e 异常
    • @param extMsg 扩展信息,给出具体的错误值信息等 */ public ServiceException(ErrorCode errorCode, Throwable e,String param[] , String ...extMsg ) { super(null==errorCode ? "" : errorCode.getCode(), e); init(errorCode, param, extMsg); }

    /**

    • @param code 错误码
    • @param msg 描述信息
    • @param e 异常 / /*public ServiceException(String code, String msg,Throwable e) { super(code+":"+msg, e); this.code = code; this.msg = msg; }/

    /**

    • 方法说明:异常错误码
    • @return */ public String getCode() { return code; }

    /**

    • 方法说明:异常描述信息
    • @return */ public String getMsg() { return msg; }

    public String getExtMsg() {
    return extMsg;
    }

    @Override
    public String getMessage() {

    return super.getMessage() + ","+extMsg;
    }

    public static void main(String[] args) {

    }

}