java运行常见异常类型

来源:文书网 8.37K

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)

热门标签