從概念角度分析:
Error:程序無法處理的系統錯誤,編譯器不做檢查;
Exception:程序可以處理的異常,捕獲后可能恢復;
總結:前者是程序無法處理的錯誤,后者是可以處理的異常。
從責任角度分析:
Error:屬於JVM需要負擔的責任;
Exception:
RuntimeException(非受檢異常)是程序應該負擔的責任;
Checked Exception (受檢異常)可檢查異常時Java編譯器應該負擔的責任。
常見Error和Exception
RuntimeException:
1、NullPropagation:空指針異常;
2、ClassCastException:類型強制轉換異常
3、IllegalArgumentException:傳遞非法參數異常
4、IndexOutOfBoundsException:下標越界異常
5、NumberFormatException:數字格式異常
非RuntimeException:
1、ClassNotFoundException:找不到指定class的異常
2、IOException:IO操作異常
Error:
1、NoClassDefFoundError:找不到class定義的異常
2、StackOverflowError:深遞歸導致棧被耗盡而拋出的異常
3、OutOfMemoryError:內存溢出異常