7.4 自定义异常类

    定义异常类只需要继承Exception类即可。

    例:自定义异常类

    Class : MyException

package limeThrowable._7_4;

public class MyException extends Exception { // 自定义异常类,继承Exception类

    /**
*
*/
private static final long serialVersionUID = 1L; // 构造方法接收异常信息
public MyException() {
super();
} public MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
} public MyException(String message, Throwable cause) {
super(message, cause);
} public MyException(String message) {
super(message);
} public MyException(Throwable cause) {
super(cause);
} }

    Class :main

package limeThrowable._7_4;

public class DefaultException {

    public static void main(String[] args) {
try{
throw new MyException("自定义异常。");
}catch (Exception e) {
System.out.println(e);
}
}
}

    Console :

limeThrowable._7_4.MyException: 自定义异常。

7.5 断言

啦啦啦

04-15 08:24