欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java異常處理詳細(xì)介紹及實(shí)例

 更新時(shí)間:2017年04月21日 09:35:40   投稿:lqh  
這篇文章主要介紹了java異常處理詳細(xì)介紹及實(shí)例的相關(guān)資料,本文對(duì)java異常進(jìn)行了知識(shí)層次的總結(jié),需要的朋友可以參考下

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

Exception異常

RuntimeException與非RuntimeException異常的區(qū)別:

非RuntimeException(檢查異常):在程序中必須使用try…catch進(jìn)行處理,否則程序無(wú)法編譯。 
RuntimeException:可以不使用try…catch進(jìn)行處理,但是如果有異常產(chǎn)生,則異常將由JVM進(jìn)行處理。
比如:我們從來(lái)沒(méi)有人去處理過(guò)NullPointerException異常,它就是運(yùn)行時(shí)異常,并且這種異常還是最常見(jiàn)的異常之一。
出現(xiàn)運(yùn)行時(shí)異常后,系統(tǒng)會(huì)把異常一直往上層拋,一直遇到處理代碼。如果沒(méi)有處理塊,到最上層,
如果是多線程就由Thread.run()拋出,如果是單線程就被main()拋出。拋出之后,如果是線程,這個(gè)線程也就退出了。
如果是主程序拋出的異常,那么這整個(gè)程序也就退出了。

Error類(lèi)和Exception類(lèi)的父類(lèi)都是throwable類(lèi),他們的區(qū)別是:

Error類(lèi)一般是指與虛擬機(jī)相關(guān)的問(wèn)題,如系統(tǒng)崩潰,虛擬機(jī)錯(cuò)誤,內(nèi)存空間不足,方法調(diào)用棧溢等。
對(duì)于這類(lèi)錯(cuò)誤的導(dǎo)致的應(yīng)用程序中斷,僅靠程序本身無(wú)法恢復(fù)和和預(yù)防,遇到這樣的錯(cuò)誤,建議讓程序終止。 
Exception類(lèi)表示程序可以處理的異常,可以捕獲且可能恢復(fù)。遇到這類(lèi)異常,應(yīng)該盡可能處理異常,
使程序恢復(fù)運(yùn)行,而不應(yīng)該隨意終止異常。

RuntimeException異常

NullPointException異常

一般報(bào)Java.lang.NullPointerException的原因有以下幾種:

1. 字符串變量未初始化;
2. 對(duì)象沒(méi)有用具體的類(lèi)初始化;

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ù)組長(zhǎng)度時(shí),就會(huì)發(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)異常的運(yùn)算條件時(shí),拋出此異常。

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)你試圖將錯(cuò)誤類(lèi)型的對(duì)象存儲(chǔ)到一個(gè)對(duì)象數(shù)組時(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ù)字時(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

類(lèi)型轉(zhuǎn)換錯(cuò)誤,通常是進(jìn)行強(qiáng)制類(lèi)型轉(zhuǎn)換時(shí)候出的錯(cuò)誤。

ClassCastException代碼如下:

/**
 * ClassCastException 父類(lèi)賦值給子類(lèi),向下轉(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)

 感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論