Java中的Throwable類是不是受查異常?

Q: Throwable是不是受查異常?
A: 是

Java規範中,對非受查異常和受查異常的定義是這樣的:

  • The unchecked exception classes are the run-time exception classes and the error 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處理。
  • 餘下的受查異常,是在編寫程序時無法提前預料到的,如文件讀寫異常、數據庫訪問異常等,這並不是程序本身的錯誤,爲了保證程序的健壯性,這些異常必須被捕獲。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章