package exception.d1.d2;
//自定义的异常类
public class Myexception extends Exception{
//传递数字>10;
private int detail;
public Myexception(int a) {
this.detail = a;
}
//toString:异常的打印信息
@Override
public String toString() {
return "Myexception{" + detail + '}';
}
}
----------------------------------------
测:
package exception.d1.d2;
public class Test {
//可能会存在异常的方法
static void test(int a) throws Myexception {
System.out.println("传递的参数为:"+a);
if(a>10){
throw new Myexception(a);//抛出
}
System.out.println("ok");
}
public static void main(String[] args) {
try {
test(1);
} catch (Myexception e) {
System.out.println("Myexception=>"+e);
}
}
}
传递的参数为:1
ok
进程已结束,退出代码为 0