Error与Exception与自定义异常
Error与Exception
Error
Exception
自定义异常
快捷键: ctrl + alt + t
package exception.demon02;
//自定义的异常类----继承Exception(异常)类
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 +
'}';
}
}
-----------------
package exception.demon02;
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);
}
}
}
学习内容源自视频:b站狂神说Java