java執行時常見異常有很多,下面總結了幾個JAVA中常見的RuntimeException:
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)