[Java] java 自定义异常类 →→→→→进入此内容的聊天室

来自 , 2020-03-01, 写在 Java, 查看 141 次.
URL http://www.code666.cn/view/f4be0027
  1. /**
  2.  * java 自定义异常类
  3.  */
  4. class MyException extends Exception {
  5.         private int detail;
  6.  
  7.         MyException(int a) {
  8.                 detail = a;
  9.         }
  10.  
  11.         public String toString() {
  12.                 return "MyException[" + detail + "]";
  13.         }
  14. }
  15.  
  16. class ExceptionDemo {
  17.         static void compute(int a) throws MyException {
  18.                 System.out.println("called compute(" + a + ").");
  19.                 if (a > 10)
  20.                         throw new MyException(a);
  21.                 System.out.println("normal exit.");
  22.         }
  23.  
  24.         public static void main(String args[]) {
  25.                 try {
  26.                         compute(1);
  27.                         compute(20);
  28.                 } catch (MyException e) {
  29.                         System.out.println("caught " + e);
  30.                 }
  31.         }
  32. }

回复 "java 自定义异常类"

这儿你可以回复上面这条便签

captcha