1. 程式人生 > 實用技巧 >Java 異常體系及 執行時異常(不需要捕捉)與一般異常有何異同(需要捕捉)

Java 異常體系及 執行時異常(不需要捕捉)與一般異常有何異同(需要捕捉)

1、Java Throable 體系圖

2、Error

  Error 是 Throwable 的子類,用於指示合理的應用程式不應該試圖捕獲的嚴重問題

3、Exception  

  • Exception 異常主要分為兩類

    • 一類是 IOException(I/O 輸入輸出異常),其中 IOException 及其子類異常又被稱作「受查異常」
    • 另一類是 RuntimeException(執行時異常),RuntimeException 被稱作「非受查異常」。
  • 受查異常就是指,編譯器在編譯期間要求必須得到處理的那些異常,你必須在編譯期處理了。

4、自定義檢查性異常和非檢查性異常

 4.1、受查異常 Checked ,繼承Exception,需要Try-Catch捕獲來處理邏輯

/**
 * @author Yungui.Zheng
 * @date 2020/08/19
 */
public class InvalidIdCardException  extends Exception {
    /**
     * use serialVersionUID from JDK 1.1.X for interoperability
     */
    private static final long serialVersionUID = 9176873029745254549L;

    /**
     * Default message
     */
    private static final String DEFAULT_MSG = "Invalid IdCard No,不合法的身份證號碼";

    /**
     * This field holds the exception ex if the
     * InvalidIdCardException(String s, Throwable ex) constructor was
     * used to instantiate the object
     * @serial
     */
    private Throwable ex;
    /**
     * Constructs a <code>InvalidIdCardException</code> with no default message.
     */
    public InvalidIdCardException() {
        super(DEFAULT_MSG);
    }

    /**
     * Constructs a <code>InvalidIdCardException</code> with specified detail message.
     */
    public InvalidIdCardException(String message) {
        super(message);
    }

    /**
     * Constructs a <code>InvalidIdCardException</code> with the
     * specified detail message and optional exception that was
     * raised while loading the class.
     *
     * @param s the detail message
     * @param ex the exception that was raised while loading the class
     */
    public InvalidIdCardException(String s, Throwable ex) {
        //  Disallow initCause
        super(s, null);
        this.ex = ex;
    }

    /**
     * Returns the exception that was raised if an error occurred while
     * attempting to load the class. Otherwise, returns <tt>null</tt>.
     *
     * <p>This method predates the general-purpose exception chaining facility.
     * The {@link Throwable#getCause()} method is now the preferred means of
     * obtaining this information.
     *
     * @return the <code>Exception</code> that was raised while loading a class
     * @since 1.2
     */
    public Throwable getException() {
        return ex;
    }

    /**
     * Returns the cause of this exception (the exception that was raised
     * if an error occurred while attempting to load the class; otherwise
     * <tt>null</tt>).
     *
     * @return  the cause of this exception.
     */
    @Override
    public Throwable getCause() {
        return ex;
    }
}

  

 4.2、非檢查性異常--不需要丟擲,繼承RuntimeException 即可,不需要Try-Catch捕獲

  
/**
 * @author Yungui.Zheng
 * @date 2020/08/19
 */
public class InvalidIdCardException  extends RuntimeException {
    /**
     * use serialVersionUID from JDK 1.1.X for interoperability
     */
    private static final long serialVersionUID = 9176873029745254549L;

    /**
     * Default message
     */
    private static final String DEFAULT_MSG = "Invalid IdCard No,不合法的身份證號碼";

    /**
     * This field holds the exception ex if the
     * InvalidIdCardException(String s, Throwable ex) constructor was
     * used to instantiate the object
     * @serial
     */
    private Throwable ex;
    /**
     * Constructs a <code>InvalidIdCardException</code> with no default message.
     */
    public InvalidIdCardException() {
        super(DEFAULT_MSG);
    }

    /**
     * Constructs a <code>InvalidIdCardException</code> with specified detail message.
     */
    public InvalidIdCardException(String message) {
        super(message);
    }

    /**
     * Constructs a <code>InvalidIdCardException</code> with the
     * specified detail message and optional exception that was
     * raised while loading the class.
     *
     * @param s the detail message
     * @param ex the exception that was raised while loading the class
     */
    public InvalidIdCardException(String s, Throwable ex) {
        //  Disallow initCause
        super(s, null);
        this.ex = ex;
    }

    /**
     * Returns the exception that was raised if an error occurred while
     * attempting to load the class. Otherwise, returns <tt>null</tt>.
     *
     * <p>This method predates the general-purpose exception chaining facility.
     * The {@link Throwable#getCause()} method is now the preferred means of
     * obtaining this information.
     *
     * @return the <code>Exception</code> that was raised while loading a class
     * @since 1.2
     */
    public Throwable getException() {
        return ex;
    }

    /**
     * Returns the cause of this exception (the exception that was raised
     * if an error occurred while attempting to load the class; otherwise
     * <tt>null</tt>).
     *
     * @return  the cause of this exception.
     */
    @Override
    public Throwable getCause() {
        return ex;
    }
}