Q: Throwable是不是受查異常?
A: 是
在Java規范中,對非受查異常和受查異常的定義是這樣的:
- The unchecked exception classes are the
run-time
exception classes and theerror
classes. - The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are Throwable and all its subclasses other than RuntimeException and its subclasses and Error and its subclasses.
也就是說,除了run-time exception
和其子類,以及error
和其子類,其它的所有異常都是受查異常。
Java中的異常分類如下:
Error
通常是一些底層的和硬件有關的錯誤,與程序本身無關,不應該被捕獲,因為捕獲了無能為力。RuntimeException
是程序本身出錯拋出的異常,這類錯誤一定是程序員本身邏輯錯誤或不嚴謹造成的,可以捕獲也可以不捕獲,如果不主動捕獲則會被JVM處理。- 余下的
受查異常
,是在編寫程序時無法提前預料到的,如文件讀寫異常、數據庫訪問異常等,這並不是程序本身的錯誤,為了保證程序的健壯性,這些異常必須被捕獲。