Error和Exception的區別:
Error:錯誤,一般是指程序或者虛擬機之類產生的無法依靠修改代碼能夠解決的問題,例如虛擬機無法正常運行等等。
典型的兩個Error
1) OutofMemoryError
2) StackOverflowError
Exception:異常,與Error最大的區別是,Exception能夠依靠修復代碼的錯誤而避免重複的問題,而Error不能。
Exception大致有以下幾種:
RuntimeException (運行時異常) 不需要try catch
NullPointerException
StringIndexOutofBounds
ArrayIndexOutofBounds
ClassCastException
CheckedException(檢查異常)
IOException
FileNotfoundException
InterruptedException
運行時異常:
不需要try catch
解決方案:通過預先判斷,規避掉問題的產生
檢查異常:
必須try catch
提示開發者寫出更健壯穩定的代碼