自定义异常类
继承Exception类
public class MyException extends Exception{ private int detail; public MyException(int a){ this.detail=a; } public String toString(){ return "MyException{"+"detail="+detail+'}'; } } 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(a:1); }catch(MyException e){ System.out.println("MyException=>"+e) } }