zoukankan      html  css  js  c++  java
  • 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());
    

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

  • 相关阅读:
    Java 数据类型转换
    Perl 日期时间函数(date time)
    find a filename from a filehandle in Perl
    Perl文件读写
    R语言算术运算和逻辑运算
    bash/shell 数学计算
    grep/awk/sed 或者 并且 否定
    关于打印 毕业设计资料
    python 捕获 shell/bash 脚本的输出结果
    推荐几个高质量的图片素材网站
  • 原文地址:https://www.cnblogs.com/JAYIT/p/5604886.html
Copyright © 2011-2022 走看看