自定义异常及经验小结
package com.Demo2;
//自定义异常类
public class MyException extends Exception {
//传递数字>10
private int detail;
public MyException(int a) {
this.detail = a;
}
//toString异常的打印信息 alt+insert-->toString
@Override
public String toString() {
return "MyException{" +
"detail=" + detail +
'}';
}
}
====================================================
package com.Demo2;
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(11);
} catch (MyException e) {
System.out.println("MyException=>"+e);
}
}
}
=========================================================