异常 自定义异常

自定义异常

 1 package exception;
 2 
 3 public class MyException extends RuntimeException {
 4 
 5     private String message;// 自定义异常信息
 6 
 7     public MyException(String message) {
 8         super(message);// 调用父类构造方法
 9     }
10 
11 }

测试

 1 package exception;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6 
 7         try {
 8 
 9             throw new MyException("自定义异常");// 抛出异常
10 
11         } catch (MyException e) {
12 
13             System.out.println("抛出异常");
14 
15             e.printStackTrace();
16 
17         }
18 
19     }
20 
21 }

猜你喜欢

转载自www.cnblogs.com/ybxxszl/p/9316629.html