- 相關(guān)推薦
java運(yùn)行常見異常舉例
下面小編為大家總結(jié)了幾個(gè)JAVA中常見的RuntimeException,希望對你們有所幫助:
NullPointerException:空指針異常類
示例1:
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)越界異常
示例2:
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ù)異常類:
示例3:
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)換異常
示例4:
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)文章:
Java編程常見問題匯總02-28
Java編寫計(jì)算器的的常見做法01-22
2017年java常見面試題及答案03-29
小升初病句修改舉例講解08-10
英語寫作常用句型舉例03-11
指甲的異常須知03-25
托福獨(dú)立寫作中如何舉例03-05
Web服務(wù)中的異常處理03-30
javascript編程異常處理的方法03-31