写一个异常类继承Exception
public class MyException extends Exception{
private int detail;
public MyException(int a) {
this.detail = a;
}
@Override
public String toString() {
return "MyException{" +
"detail=" + detail +
'}';
}
}
public class MyExceptionTest {
//可能存在异常的方法
static void test(int a) throws MyException {
if(a>10){
throw new MyException(a);
}
System.out.println("OK");
}
public static void main(String[] args) {
try {
test(11);
} catch (MyException e) {
System.out.println(e);//MyException{detail=11}
}
}
}