Java異常層次結構
Exception異常
RuntimeException與非RuntimeException異常的區別:
1
2
3
4
5
6
|
非RuntimeException(檢查異常):在程序中必須使用 try … catch 進行處理,否則程序無法編譯。 RuntimeException:可以不使用 try … catch 進行處理,但是如果有異常產生,則異常將由JVM進行處理。 比如:我們從來沒有人去處理過NullPointerException異常,它就是運行時異常,并且這種異常還是最常見的異常之一。 出現運行時異常后,系統會把異常一直往上層拋,一直遇到處理代碼。如果沒有處理塊,到最上層, 如果是多線程就由Thread.run()拋出,如果是單線程就被main()拋出。拋出之后,如果是線程,這個線程也就退出了。 如果是主程序拋出的異常,那么這整個程序也就退出了。 |
Error類和Exception類的父類都是throwable類,他們的區別是:
1
2
3
4
|
Error類一般是指與虛擬機相關的問題,如系統崩潰,虛擬機錯誤,內存空間不足,方法調用棧溢等。 對于這類錯誤的導致的應用程序中斷,僅靠程序本身無法恢復和和預防,遇到這樣的錯誤,建議讓程序終止。 Exception類表示程序可以處理的異常,可以捕獲且可能恢復。遇到這類異常,應該盡可能處理異常, 使程序恢復運行,而不應該隨意終止異常。 |
RuntimeException異常
NullPointException異常
一般報Java.lang.NullPointerException的原因有以下幾種:
1. 字符串變量未初始化;
2. 對象沒有用具體的類初始化;
NullPointException代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package TestNullPointException; public class TestNullPointException { public static void main (String[] args) { String str = null ; try { if (str.equals( null )) { System.out.println( "true" ); } else { System.out.println( "false" ); } } catch (NullPointException e) { e.printStackTrace(); } } } |
輸出:
1
2
|
java.lang.NullPointerException at TestNullPointException.TestNullPointException.main(TestNullPointException.java: 6 ) |
ArrayIndexOutOfBoundsException異常
數組下標越界異常,當引用的索引值超出數組長度時,就會發生此異常。
ArrayIndexOutOfBoundsException 代碼如下:
1
2
3
4
5
6
7
8
9
10
11
|
package TestArrayIndexOutOfBoundsException; public class TestArrayIndexOutOfBoundsException { public static void main (String[] args) { Integer[] array = new Integer[ 10 ]; try { Integer temp = array[ 10 ]; } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); } } } |
輸出:
1
2
|
java.lang.ArrayIndexOutOfBoundsException: 10 at TestArrayIndexOutOfBoundsException.TestArrayIndexOutOfBoundsException.main(TestArrayIndexOutOfBoundsException.java: 6 ) |
ArithmeticException
ArithmeticException是出現異常的運算條件時,拋出此異常。
ArithmeticException代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/** * ArithmeticException */ packet TestArithmeticException; public class TestArithmeticException { public static void main(String[] args) { Integer temp = 1 ; try { System.out.println(temp/ 0 ); } catch (ArithmeticException e) { e.printStackTrace(); } } } |
輸出:
1
2
|
java.lang.ArithmeticException: / by zero at TestArithmeticException.TestArithmeticException.main(TestArithmeticException.java: 6 ) |
ArrayStoreException
當你試圖將錯誤類型的對象存儲到一個對象數組時拋出的異常。
ArrayStoreException代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/** * ArrayStoreException */ packet TestArrayStoreException; public class TestArrayStoreException { public static void main(String[] args) { Object array = new Integer[ 10 ]; try { array[ 0 ] = "123" ; } catch (ArrayStoreException e) { e.printStackTrace(); } } } |
輸出:
1
2
|
Exception in thread "main" java.lang.ArrayStoreException: java.lang.String at TestArrayStoreException.TestArrayStoreException.main(TestArrayStoreException.java: 6 ) |
NumberFormatException
繼承IllegalArgumentException,字符串轉換為數字時出現。
NumberFormatException代碼如下:
1
2
3
4
5
6
7
8
9
10
|
/** * NumberFormatException */ package test; public class ExceptionTest { public static void main(String[] args) { String s = "q12" ; Integer i = Integer.parseInt(s); } } |
輸出:
1
2
3
4
5
|
Exception in thread "main" java.lang.NumberFormatException: For input string: "q12" at java.lang.NumberFormatException.forInputString(NumberFormatException.java: 65 ) at java.lang.Integer.parseInt(Integer.java: 580 ) at java.lang.Integer.parseInt(Integer.java: 615 ) at test.ExceptionTest.main(ExceptionTest.java: 8 ) |
ClassCastException
類型轉換錯誤,通常是進行強制類型轉換時候出的錯誤。
ClassCastException代碼如下:
1
2
3
4
5
6
7
8
9
10
|
/** * ClassCastException 父類賦值給子類,向下轉型 */ package test; public class ExceptionTest { public static void main(String[] args) { Object obj= new Object(); Integer s=(Integer)obj; } } |
輸出:
1
2
|
Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.Integer at test.ExceptionTest.main(ExceptionTest.java: 5 ) |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/songfeihu0810232/article/details/68948874