JAVA認證

當前位置 /首頁/IT認證/JAVA認證/列表

java執行常見異常型別

JAVA執行時常見異常有很多,下面總結了幾個JAVA中常見的RuntimeException:

java執行常見異常型別

  NullPointerException:空指標異常類

示例

package c;

public class Test {

public static void main(String[] args) {

tln(toUpper(null));

}

public static String toUpper(String str){

return perCase();

}

}

異常資訊如下:Exception in thread “main” PointerException

at per(:11)

at (:6)

  ArrayIndexOutOfBoundsException:陣列下標越界異常

示例:

package c;

public class Test {

public static void main(String[] args) {

int[] a = {0,1,2,3};

tln(a[4]);

}

}

異常資訊如下:

Exception in thread “main” yIndexOutOfBoundsException: 4

at (:7)

  ArithmeticExecption:算術異常類:

示例:

package c;

public class Test {

public static void main(String[] args) {

int a = 10 / 0;

tln(a);

}

}

異常資訊如下:

Exception in thread “main” hmeticException: / by zero

at (:6)

  ClassCastException:型別強制轉換異常

示例:

package c;

public class Test {

public static void main(String[] args) {

testParse(“aaa”);

}

public static void testParse(Object str){

Integer i = (Integer)str;

}

}

異常資訊如下:

Exception in thread “main” sCastException: ng cannot be cast to ger

at Parse(:10)

at (:6)

TAG標籤:執行 JAVA #