// 自定义的异常类
public class MyException extends Exception{// 传递数字 > 10private int detail;public MyException(int a) {this.detail = a;}// toString: 异常的打印信息@Overridepublic String toString() {return "MyException{" + detail + '}';}
}
public class Test {// 可能会存在异常的方法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(13);} catch (MyException e) {System.out.println("MyException=>"+e);}System.out.println("==========");}
}
https://www.bilibili.com/video/BV12J41137hu?p=79&spm_id_from=pageDriver