1、 Java.lang.NullPointerException(空指针异常)
调用了未经初始化的对象或者是不存在的对象
public class Test {
@SuppressWarnings("null")
public static void main(String[] args){
Test test = null;
test.eat();
}
public void eat(){
};
}
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:5)
2、 java.lang.ClassNotFoundException
指定的类不存在
public class Test {
public static void main(String[] args){
try {
Class<?> forName = Class.forName("A");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
java.lang.ClassNotFoundException: A
3、 java.lang.NumberFormatException
字符串转换为数字异常
public class Test {
public static void main(String[] args){
int num = Integer.parseInt(".10");
}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: ".10"
4、java.lang.IndexOutOfBoundsException
数组下标越界异常
public class Test {
public static void main(String[] args){
int[] array = new int[1];
int i = array[3];
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Test.main(Test.java:4)
5、java.lang.IllegalArgumentException
方法的参数错误
import java.awt.Color;
public class Test {
public static void main(String[] args){
Color color = new Color(256, 1, 1);
}
}
Exception in thread "main" java.lang.IllegalArgumentException: Color parameter outside of expected range: Red
6、java.lang.IllegalAccessException
没有访问权限
public class Test {
public static void main(String[] args){
try {
try {
A a = (A) Class.forName("A").newInstance();
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
class A{
private A(){
}
}
java.lang.IllegalAccessException: Class Test can not access a member of class A with modifiers "private"
7、java.lang.ArithmeticException
数学运算异常
public class Test {
public static void main(String[] args){
int i = 9/0;
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
8、java.lang.ClassCastException
类型转换异常
public class Test {
public static void main(String[] args){
Test a = new A();
B b = (B)a;
}
}
class A extends Test{
}
class B extends Test{
}
Exception in thread "main" java.lang.ClassCastException: A cannot be cast to B
9、 java.lang.FileNotFoundException
文件未找到异常
public class Test {
public static void main(String[] args){
File file = new File("c:");
try {
FileInputStream fileInputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
java.io.FileNotFoundException: c: (拒绝访问。)
10、java.lang.ArrayStoreException
数组存储异常
public class Test {
public static void main(String[] args){
Object[] array = new String[10];
array[0] = new Integer(1);
}
}
Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer