当前位置:才华斋>IT认证>JAVA认证>

java运行常见异常举例

JAVA认证 阅读(5.02K)

下面小编为大家总结了几个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)