自定义异常类

package com.guoba.exception;
/*
自定义异常类
 */
public class LiXuanException extends Exception{
    private int detail;

    public LiXuanException(int e){
        this.detail = e;
    }

    @Override
    public String toString() {
        return "LiXuanException{" +
                "detail=" + detail +
                '}';
    }
}
  • 测试类
package com.guoba.exception;

public class test {
    //可能会异常的方法
    static void test(int e)throws LiXuanException{
        System.out.println("传递的参数为"+e);
        if (e>10){
            throw new LiXuanException(e);//抛出

        }
        System.out.println("ok");
    }

    public static void main(String[] args) {
        try {
            test(11);
        } catch (LiXuanException e) {
            System.out.println("LixuanException=>"+e);
        }
    }
}
posted @ 2021-12-13 20:29  锅巴编程  阅读(32)  评论(0编辑  收藏  举报