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-cache」結構或是「throws」關鍵字來處理例外,否則程式會無法編譯成功,而不需檢查的例外則不管有沒有寫程式去處理都可以編譯成功。

不需檢查的例外包含RuntimeException、Error以及所有其它繼承它們的例外(或是錯誤),常見的有ArrayIndexOutOfBoundsException和NullPointerException。其餘的例外都是需要檢查的例外,常見的有IOException和SQLException。

選項A全都是不需檢查的例外。

選項B的IOException是需要檢查的例外。

選項C的FileNotFoundException是需要檢查的例外。

選項D全都是需要檢查的例外。

選項E的FileNotFoundException是需要檢查的例外。