Java異常層次結(jié)構(gòu)

Exception異常
RuntimeException與非RuntimeException異常的區(qū)別:
非RuntimeException(檢查異常):在程序中必須使用try…catch進行處理,否則程序無法編譯。 RuntimeException:可以不使用try…catch進行處理,但是如果有異常產(chǎn)生,則異常將由JVM進行處理。比如:我們從來沒有人去處理過NullPointerException異常,它就是運行時異常,并且這種異常還是最常見的異常之一。出現(xiàn)運行時異常后,系統(tǒng)會把異常一直往上層拋,一直遇到處理代碼。如果沒有處理塊,到最上層,如果是多線程就由Thread.run()拋出,如果是單線程就被main()拋出。拋出之后,如果是線程,這個線程也就退出了。如果是主程序拋出的異常,那么這整個程序也就退出了。
Error類和Exception類的父類都是throwable類,他們的區(qū)別是:
Error類一般是指與虛擬機相關(guān)的問題,如系統(tǒng)崩潰,虛擬機錯誤,內(nèi)存空間不足,方法調(diào)用棧溢等。對于這類錯誤的導(dǎo)致的應(yīng)用程序中斷,僅靠程序本身無法恢復(fù)和和預(yù)防,遇到這樣的錯誤,建議讓程序終止。 Exception類表示程序可以處理的異常,可以捕獲且可能恢復(fù)。遇到這類異常,應(yīng)該盡可能處理異常,使程序恢復(fù)運行,而不應(yīng)該隨意終止異常。
RuntimeException異常
NullPointException異常
一般報Java.lang.NullPointerException的原因有以下幾種:
1. 字符串變量未初始化;
2. 對象沒有用具體的類初始化;
NullPointException代碼如下:
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(); } }}輸出:
java.lang.NullPointerException at TestNullPointException.TestNullPointException.main(TestNullPointException.java:6)
ArrayIndexOutOfBoundsException異常
數(shù)組下標(biāo)越界異常,當(dāng)引用的索引值超出數(shù)組長度時,就會發(fā)生此異常。
ArrayIndexOutOfBoundsException 代碼如下:
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(); } }}輸出:
java.lang.ArrayIndexOutOfBoundsException: 10 at TestArrayIndexOutOfBoundsException.TestArrayIndexOutOfBoundsException.main(TestArrayIndexOutOfBoundsException.java:6)
ArithmeticException
ArithmeticException是出現(xiàn)異常的運算條件時,拋出此異常。
ArithmeticException代碼如下:
/** * 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(); } } }輸出:
java.lang.ArithmeticException: / by zero at TestArithmeticException.TestArithmeticException.main(TestArithmeticException.java:6)
ArrayStoreException
當(dāng)你試圖將錯誤類型的對象存儲到一個對象數(shù)組時拋出的異常。
ArrayStoreException代碼如下:
/** * 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(); } } }輸出:
Exception in thread "main" java.lang.ArrayStoreException: java.lang.String at TestArrayStoreException.TestArrayStoreException.main(TestArrayStoreException.java:6)
NumberFormatException
繼承IllegalArgumentException,字符串轉(zhuǎn)換為數(shù)字時出現(xiàn)。
NumberFormatException代碼如下:
/** * NumberFormatException */package test;public class ExceptionTest { public static void main(String[] args) { String s = "q12"; Integer i = Integer.parseInt(s); } }輸出:
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
類型轉(zhuǎn)換錯誤,通常是進行強制類型轉(zhuǎn)換時候出的錯誤。
ClassCastException代碼如下:
/** * ClassCastException 父類賦值給子類,向下轉(zhuǎn)型 */package test;public class ExceptionTest { public static void main(String[] args) { Object obj=new Object(); Integer s=(Integer)obj; } }輸出:
Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.Integer at test.ExceptionTest.main(ExceptionTest.java:5)
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答
圖片精選