java運行常見異常舉例

下面小編爲大家總結了幾個JAVA中常見的RuntimeException,希望對你們有所幫助:

java運行常見異常舉例

  NullPointerException:空指針異常類

示例1:

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:數組下標越界異常

示例2:

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:算術異常類:

示例3:

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:類型強制轉換異常

示例4:

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)