- 相關(guān)推薦
java運(yùn)行常見異常類型
java運(yùn)行時常見異常有很多,下面總結(jié)了幾個JAVA中常見的RuntimeException:
NullPointerException:空指針異常類
示例:
package com.darkmi.basic;
public class Test {
public static void main(String[] args) {
System.out.println(toUpper(null));
}
public static String toUpper(String str){
return str.toUpperCase();
}
}
異常信息如下:Exception in thread “main” java.lang.NullPointerException
at com.darkmi.basic.Test.toUpper(Test.java:11)
at com.darkmi.basic.Test.main(Test.java:6)
ArrayIndexOutOfBoundsException:數(shù)組下標(biāo)越界異常
示例:
package com.darkmi.basic;
public class Test {
public static void main(String[] args) {
int[] a = {0,1,2,3};
System.out.println(a[4]);
}
}
異常信息如下:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 4
at com.darkmi.basic.Test.main(Test.java:7)
ArithmeticExecption:算術(shù)異常類:
示例:
package com.darkmi.basic;
public class Test {
public static void main(String[] args) {
int a = 10 / 0;
System.out.println(a);
}
}
異常信息如下:
Exception in thread “main” java.lang.ArithmeticException: / by zero
at com.darkmi.basic.Test.main(Test.java:6)
ClassCastException:類型強(qiáng)制轉(zhuǎn)換異常
示例:
package com.darkmi.basic;
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” java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at com.darkmi.basic.Test.testParse(Test.java:10)
at com.darkmi.basic.Test.main(Test.java:6)
【java運(yùn)行常見異常類型】相關(guān)文章:
常見的廣告招牌類型05-07
Java基本數(shù)據(jù)類型07-09
常見樂器類型的EQ特性04-12
汽車不必維修的10個常見異常07-16
常見出國留學(xué)考試類型10-11
Java中基本數(shù)據(jù)類型與應(yīng)用08-23
中考化學(xué)推斷題的常見類型與解法08-23
出國留學(xué)常見的八種考試類型02-20