自定义Exception的创建

本文介绍了一个自定义异常类CredtiException的实现方法,包括多种构造函数的使用,以及如何设置错误码。此外还概述了如何在Java程序中声明、抛出和捕获异常。

/**

 * 异常类

 * 

 * @author

 * @version 2014-08-06

 */

public class CredtiException extends Exception {

    /**

     * 

     */

    private static final long serialVersionUID = xxx;

 

    private String            errorCode        = null;

 

    /**

     * <默认构造函数>

     */

    public CredtiException() {

        super();

    }

 

    /**

     * 构造函数

     * 

     * @param message 错误信息

     */

    public CredtiException(String message) {

        super(message);

        setErrorCode(message);

    }

 

    /**

     * 构造函数

     * 

     * @param message 错误信息

     * @param errorCode 错误码

     */

    public CredtiException(String message, String errorCode) {

        super(message);

        setErrorCode(errorCode);

    }

 

    /**

     * 构造函数

     * 

     * @param t Throwable

     */

    public CredtiException(Throwable t) {

        super(t);

    }

 

    /**

     * 构造函数

     * 

     * @param t Throwable

     * @param errorCode 错误码

     */

    public CredtiException(Throwable t, String errorCode) {

        super(t);

        setErrorCode(errorCode);

    }

 

    public String getErrorCode() {

        return errorCode;

    }

 

    public void setErrorCode(String errorCode) {

        this.errorCode = errorCode;

    }

}

 

 

  1. 1.自定义异常: 
  2.  
  3. class 异常类名 extends Exception 
  4. { 
  5.     public 异常类名(String msg) 
  6.     { 
  7.         super(msg); 
  8.     } 
  9.  
  10.  
  11. 2.标识可能抛出的异常: 
  12.  
  13. throws 异常类名1,异常类名2 
  14.  
  15. 3.捕获异常: 
  16. try{} 
  17. catch(异常类名 y){} 
  18. catch(异常类名 y){} 
  19.  
  20. 4.方法解释 
  21. getMessage() //输出异常的信息 
  22. printStackTrace() //输出导致异常更为详细的信息 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值