Given the classes:
* AssertionError
* ArithmeticException
* ArrayIndexOutofBoundsException
* FileNotFoundException
* IllegalArgumentException
* IOError
* IOException
* NumberFormatException
* SQLException
Which option lists only those classes that belong to the unchecked exception category?
A. AssertionError, ArrayIndexOutOfBoundsException, ArithmeticException
B. AssertionError, IOError, IOException
C. ArithmeticException, FileNotFoundException, NumberFormatException
D. FileNotFoundException, IOException, SQLException
E. ArrayIndexOutOfBoundException, IllegalArgumentException, FileNotFoundException
題解
Java 將例外狀況分成兩種類型,一為需要檢查的例外(checked exception),另一為不需檢查的例外(unchecked exception)。
需要檢查和不需檢查的例外差別在於撰寫程式的時候,需要檢查的例外必須使用「try-catch」結構或是「throws」關鍵字來處理例外,否則程式會無法編譯成功,而不需檢查的例外則不管有沒有寫程式去處理都可以編譯成功。
不需檢查的例外包含 RuntimeException、Error 以及所有其它繼承它們的例外(或是錯誤),常見的有 ArrayIndexOutOfBoundsException 和 NullPointerException。其餘的例外都是需要檢查的例外,常見的有 IOException 和 SQLException。
選項 A 全都是不需檢查的例外。
選項 B 的 IOException 是需要檢查的例外。
選項 C 的 FileNotFoundException 是需要檢查的例外。
選項 D 全都是需要檢查的例外。
選項 E 的 FileNotFoundException 是需要檢查的例外。
