1. 程式人生 > >java的錯誤分類

java的錯誤分類

技術 應用 dex 不能 數據 abcde exc span ams

java的錯誤分類


java中的錯誤分為兩大類:Error和Exception錯誤。

技術分享圖片
Error 是程序無法處理的錯誤,表示運行應用程序中較嚴重問題,修改程序本身是不能解決的。例如java運行時產生的系統內部錯誤(虛擬機錯誤),資源耗盡導致的錯誤。

Exception是異常類,它表示程序本身可以處理的錯誤。主要分為非運行異常和運行異常。
1.非運行時異常(編譯異常)
IOException異常
表示由失敗或中斷的I / O操作產生的異常類。
SQLException異常


表示訪問數據庫錯誤或者數據庫信息錯誤。

2.運行時異常(RuntimeException)
常見的運行時異常:
ClassCastException(類轉換異常) public class Demo{ public static void main(String[] args){ Object x = new Integer(0); System.out.println((String)x); } }

StringIndexOutOfBoundsException(字符串角標越界異常)

public class Demo { public
static void main(String[] args){ String s = "abcdefg"; System.out.print(s.charAt(9)); } }

ArrayIndexOutOfBoundsException(數組角標越界異常)

public class Demo { public static void main(String[] args){ int [] arr = new int[4]; System.out.println("arr[0]="+arr[4]); } }

NullPointerException(空指針異常)

public class HelloWorld{ public static void main(String[] args){ int [] arr = new int[4]; arr[0] = 5; System.out.println("arr[0]="+arr[0]);
arr = null; System.out.println("arr[0]="+arr[0]);//報錯 } }

ArrayStoreException(數據存儲異常,操作數組時類型不一致)

public class Test{ public static void main(String[] args){ Object x[] = new String[3]; x[0] = new Integer(0); System.out.println(x[0]); } }

ArithmeticException(算術異常)

public class FindError { public static void main(String[] args){ int x = 4,y = 0; int result = x / y; System.out.print(result); } }

java的錯誤分類