Error Exception

原文地址:http://www.cnblogs.com/with-wang/archive/2012/03/24/java_doc_3.htmlhtml

關係java



2)  再看看JDK中對於java.lang.Error和java.lang.Exception的解釋。
    java.lang.Error: An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. 即:Error是Throwable的子類,用於標記嚴重錯誤。合理的應用程序不該該去try/catch這種錯誤。絕大多數的錯誤都是非正常的,就根本不應出現的。    java.lang.Exception: The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch. 即Exception 是Throwable的一種形式的子類,用於指示一種合理的程序想去catch的條件。即它僅僅是一種程序運行條件,而非嚴重錯誤,而且鼓勵用戶程序去catch它。

3)  Error和RuntimeException 及其子類都是未檢查的異常(unchecked exceptions),而全部其餘的Exception類都是檢查了的異常(checked exceptions).
checked exceptions: 一般是從一個能夠恢復的程序中拋出來的,而且最好可以從這種異常中使用程序恢復。好比FileNotFoundException, ParseException等。
unchecked exceptions: 一般是若是一切正常的話本不應發生的異常,可是的確發生了。好比ArrayIndexOutOfBoundException, ClassCastException等。從語言自己的角度講,程序不應去catch這類異常,雖然可以從諸如RuntimeException這樣的異常中catch並恢復,可是並不鼓勵終端程序員這麼作,由於徹底沒要必要。由於這類錯誤自己就是bug,應該被修復,出現此類錯誤時程序就應該當即中止執行。 所以,面對Errors和unchecked exceptions應該讓程序自動終止執行,程序員不應作諸如try/catch這樣的事情,而是應該查明緣由,修改代碼邏輯。程序員

相關文章
相關標籤/搜索