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());
类似上面这种,这样我们就可以根据抛出的错误码判断是哪个地方出问题了。
作者: lost blog
出处: http://www.cnblogs.com/JAYIT/
关于作者:专注服务器端开发
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接 如有问题, 可邮件(sawyershaw@qq.com)咨询.