java自定义异常

package pay.common.exception;

@SuppressWarnings("serial")
public class PayException extends Exception{
	 private int ExceptionId;//错误码
	 private String info; //错误信息
	 public PayException(int exceptionId, String info) {
		super();
		ExceptionId = exceptionId;
		this.info = info;
	}
	 public PayException(int exceptionId) {
		 ExceptionId = exceptionId;
		}
	
	 public PayException(String info) {
		super(info);
		this.info = info;
	}	
	 public String toString()
	    {
	        return (new StringBuilder()).append("PayException,CODE=").append(ExceptionId).toString();
	    }

	public String getInfo() {
		return info;
	}
	
	 public int getExceptionId() {
		return ExceptionId;
	}
	 
	public static byte INTERNAL = -2;//内部错误
	public static byte MDB_INTERNAL = -6;//数据库错误
}

  需要继承Exception类

然后某个类要用这个异常类时先throws Exception

然后

} catch (HttpException e) {
			throw new PayException(PayException.INTERNAL,e.getMessage());
		} catch (IOException e) {
			postMethod.releaseConnection();// 考虑长连接?
			SimpleHttpConnectionManager connM = (SimpleHttpConnectionManager) httpClient
					.getHttpConnectionManager();
			connM.closeIdleConnections(0);
			throw new PayException(PayException.INTERNAL,e.getMessage());

 类似上面这种,这样我们就可以根据抛出的错误码判断是哪个地方出问题了。

posted @ 2016-06-21 20:12  Lost blog  阅读(224)  评论(0编辑  收藏  举报