java的錯誤分類

來源:互聯網
上載者:User

標籤:alt   stat   虛擬   ram   not   comment   sql   exce   ring   

 

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(null 指標異常)

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的錯誤分類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.