自定义异常

自定义异常类,一般通过继承Exception或者它的某个子类。
如果父类是RuntimeException或它的某个子类,则自定义异常也是unchecked exception。如果是Exception或Exception的其他子类,则自定义异常是checked exception。

继承Exception的栗子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class AppException extends Exception{

public AppException() {
super();
}

public AppException(String message, Throwable cause) {
super(message,cause);
}

public AppException(String message) {
super(message);
}

public AppException(Throwable cause) {
super(cause);
}
}

AppException没有定义额外的属性和代码,只是继承了Exception,定义了构造方法并调用了父类的构造方法。

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2021 Silver Shaded
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信