1.NullPointerException: 空指针异常
举例:
public static void main(String[] args) {String str = null;//此处报空指针异常System.out.println(str.length());}
控制台输出的异常信息为:
Exception in thread "main" java.lang.NullPointerExceptionat cn.com.gjw.MyClass.main(MyClass.java:7)
2.ClassCastException: 类型强制转换异常
举例:
public static void main(String[] args) {// 类型强制转换异常Object x = new String("String");System.out.println((Integer) x);}
控制台输出的异常信息为:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integerat cn.com.gjw.MyClass.main(MyClass.java:7)
3.ArrayIndexOutOfBoundsException: 数组下标越界异常
举例:
public static void main(String[] args) {int arr[] = {1,2};// 此处报数组下标越界异常System.out.println(arr[2]);}
控制台输出的异常信息为:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2at cn.com.gjw.MyClass.main(MyClass.java:7)
4.ArithmeticException:算术运算异常
举例:
public static void main(String[] args) {// 整数0做了分母,报算术运算异常System.out.println(1 / 0);}
控制台输出的异常信息为:
Exception in thread "main" java.lang.ArithmeticException: / by zeroat cn.com.gjw.MyClass.main(MyClass.java:6)
5.NumberFormatException: 数字格式异常
举例:
public static void main(String[] args) {// 将字符串“it”转换为Integer类型的,当然会报数字格式异常啦System.out.println(Integer.parseInt("it"));}
控制台输出的异常信息为:
Exception in thread "main" java.lang.NumberFormatException: For input string: "it"at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at java.lang.Integer.parseInt(Integer.java:492)at java.lang.Integer.parseInt(Integer.java:527)at cn.com.gjw.MyClass.main(MyClass.java:6)
推荐教程:Java教程