自定义异常
//自定义异常MyException
public class MyException extends Exception{
//传递数字>10抛出异常
private int detail;
public MyException (int a){
this.detail = a;
}
//toString 用于打印异常
@Override
public String toString() {
return "MyException{"+"detail="+detail+"}";
}
}
*************************************************
public class Test {
static void test(int a)throws MyException{
System.out.println("a="+a);
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("MyException="+e);
}
}
}