1. 程式人生 > >java常見異常和程式碼演示

java常見異常和程式碼演示

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