天天看點

Java基礎之異常詳解面試提問

面試提問

  1. Exception和Error的差別
  2. 運作時異常和受檢異常的差別
  3. 寫出幾種常見的運作時異常

Exception和Error的差別

Java基礎之異常詳解面試提問
Java基礎之異常詳解面試提問

Error和Exception都繼承自Throwable。

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java {@code throw} statement. Similarly, only this class or one of its subclasses can be the argument type in a {@code catch} clause.

Throwable類是Java語言中所有錯誤和異常的超類。 Java虛拟機僅抛出屬于此類(或其子類之一)的執行個體的對象,或者Java {@code throw}語句可以抛出該對象。 同樣,在{@code catch}子句中,隻有此類或其子類之一才能成為參數類型。

 Throwable是所有異常、Error的基類。 隻有由Throwable衍生出來的子類才能被程式抛出,并且能被程式catch到,如果一個類和Throwable不是繼承的關系的話,那麼是不能去throw它,也不能去catch它的。

Error解讀:

 An {@code Error} is a subclass of {@code Throwable} that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The {@code ThreadDeath} error, though a "normal" condition, is also a subclass of {@code Error} because most applications should not try to catch it.

{@code Error}是{@code Throwable}的子類,表示合理的應用程式不應嘗試捕獲的嚴重問題。 大多數此類錯誤是異常情況。 {@code ThreadDeath}錯誤雖然是“正常”情況,但它也是{@code Error}的子類,因為大多數應用程式不應嘗試捕獲它。

 Error是不應該被catch掉的,Error通常情況下是指在我們程式正常運作的時候它不大可能出現情況,當Error出現的時候,一般會導緻我們的程式崩潰,并且處于一個不可恢複的狀态,比如說:OutOfMemoryError,這個時候程式已經崩潰了,是不可能通過捕獲異常來進行恢複的,是以說我們在程式中是不應該捕獲這些嚴重的錯誤的。

Exception解讀:

The class {@code Exception} and its subclasses are a form of {@code Throwable} that indicates conditions that a reasonable application might want to catch.

{@code Exception}類及其子類是{@code Throwable}的一種形式,它表示合理的應用程式可能希望捕獲的條件。

“might want to catch”異常是我們推薦去捕獲并且處理它的,Exception就是我們程式在運作的時候可以預見的一些意外的情況,比如說參數為空、參數類型不合法。此時我們可以對它進行判斷,捕捉并處理。

 關于Exception和Error的差別的标準回答:

     Exception和Error都繼承自Throwable,在Java中隻有Throwable類型的執行個體才可以被抛出或捕獲。

     Error指正常情況下不太可能出現的情況,絕大部分的Error會導緻程式崩潰,處于非正常的不可恢複的狀态,如OutOfMemoryError、      StackOverFlowError,是程式中不應該試圖捕獲的嚴重問題。

     Exception是程式正常運作中可以預料的意外問題,可以捕獲并處理。

The Exception class  and any subclasses that are not also subclasses of RuntimeException are  checked exceptions.  Checked exceptions need to be declared in a method or constructor's {@code throws} clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

 受檢異常就是編譯時被強制檢查的異常,我們在方法申明的時候要去申明這些異常,常見的受檢異常比如說:SQLException,IOException、FileNotFoundException。 我們調用這些抛出受檢異常的方法的時候,調用者需要對抛出的異常進行捕獲處理或繼續往上抛出的。

其他的異常稱為不受檢查的異常,不受檢查的異常又稱為運作時異常,這些異常隻有在程式運作的時候才會出現,所有的運作時異常都是繼承自RuntimeException的,比如說空指針異常,NullPointerException,還有類型轉換異常ClassCastException,還有我們經常遇到的數組越界異常,IndexOutOfBoundsException、ClassNotFoundException異常,不受檢查的異常通常是在編碼中可以避免的邏輯錯誤。

代碼解釋:

public static void test() throws NullPointerException {
   System.out.println("you are best one!");
}
public static void main(String[] args) {
   //對于test方法抛出的運作時異常,不需要強制要求處理,
   // 而如果抛出的是受檢異常(如SQLException)則強制要求處理!
   SingletonCAS.test();
}
           

 關于運作時異常和受檢異常的差別的标準回答:

      受檢異常:在編譯時被強制檢查的異常,在方法的聲明中throws聲明的異常。(舉例:ClassNotFoundException、IOException)

      不受檢異常:也稱為運作時異常,通常是在編碼中可以避免的邏輯錯誤,根據需求來判斷如何處理,不需要在編譯器期間強制處理。

關于寫出幾種常見的運作時異常的标準回答:

      運作時異常RuntimeException是所有不受檢查異常的基類,NullPointerException、ClassCastException、NumberFormatException、ArrayIndexOutOfBoundsException等。

Throwable的大緻分類:

Java基礎之異常詳解面試提問