自定义异常

//自定义异常类
public class MyException extends Exception{
   //传递数字》10
   private int detail;

   public MyException(int a) {
       this.detail = a;
  }

   @Override
   public String toString() {
       return "MyException{" + detail + '}';
  }
}
=================================================
public class Test1 {
   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(1);//alt+enter 自动提示错误
      } catch (MyException e) {
           System.out.println("MyException=>"+e);
      }
  }
}
 
posted @ 2024-01-24 09:49  fightownself  阅读(1)  评论(0编辑  收藏  举报