java # 自定义异常


自定义异常

 

package com.study.exception;
//自定义的异常类
public class text3 extends Exception{
    //传递数字>10
    private int detail;

    public text3( int a) {

        this.detail = a;
    }

    //toString:异常的打印信息
    @Override
    public String toString() {
        return "text3{" +
                "detail=" + detail +
                '}';
    }
}
package com.study.exception;

public class text4 {
    static void test( int a) throws text3 {
        System.out.println("传递的参数为:"+a);
        if(a>10){
            throw new text3(a);
        }
        System.out.println("ok");
    }

    public static void main(String[] args) {
        try {
            test(11);
        } catch (com.study.exception.text3 text3) {
            System.out.println("text3"+text3);
        }
    }

}

2.

 

相关