常见的异常有哪些?

/**
         * java.lang.Throwable
         *     |------java.lang.Error:一般不编写针对性代码
         *     |------java.lang.Exception:可以进行异常处理的类型
         *           |--------编译时异常(checked)
         *                  |--------IOException
         *                          |--------FileNoFoundException
         *                  |--------ClassNotFoundException
         *           |--------运行时异常(unchecked)
         *                  |--------NullPointException
         *                  |--------ArrayIndexOutOfBoundsException
         *                  |--------ClassCastException
         *                  |--------NumberFormatException
         *                  |--------InputMismatchException
         *                  |--------ArithmeticException
         * @param args
         */

 

运行时异常

1、空指针异常(NullPointException):当对象不存在,却又去调用对象的属性或方法时,就是出现该。

public static void main(String[] args) {
        User user=null;
        System.out.println(user.getAge());
    }

输出:

Exception in thread "main" java.lang.NullPointerException
    at com.exambner.exception.TextException.main(TextException.java:14)

2、数组越界异常(ArrayIndexOutOfBoundsException):当数组只存在5个元素,他们所对应的的下标即为0-4,如果访问数组下标为5的元素时,就会提示该异常,原因是该位置元素不存在。

 public static void main(String[] args) {
        String[] array = new String[5];
        System.out.println(array[5]);
    }

输出:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at com.exambner.exception.TextException.main(TextException.java:14)

3、强制类型转换异常(ClassCastException):在进行类型转换时,如果两个对象类型不匹配,无法进行转换时,就会出现该异常。

public static void main(String[] args) {
        Object o = new Date();
        String str = (String) o;
    }

输出:

Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String
    at com.exambner.exception.TextException.main(TextException.java:16)

4、数字格式化异常(NumberFormatException):在进行数组类型转换时,因为格式的原因以至于无法正常转换的异常。

public static void main(String[] args) {
        String str = "1.1.1";
        Integer num = Integer.valueOf(str);
    }

输出:

Exception in thread "main" java.lang.NumberFormatException: For input string: "1.1.1"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.valueOf(Integer.java:766)
    at com.exambner.exception.TextException.main(TextException.java:16)

Process finished with exit code 1

5、输入类型不匹配异常(InputMismatchException):使用Scanner时输入的类型和接收输入的类型不一致导致。

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Integer s = scanner.nextInt();
        System.out.println(s);
    }

这边我们定义去用数值类型接收,也就是希望用户输入一个数值类型,假如我们输入其他类型就会提示异常。

输出:

输入aaa。
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at com.exambner.exception.TextException.main(TextException.java:18)

5、算数运算符异常(ArithmeticException):出现异常的运算条件时,会出现该异常。例如:3/0;

public static void main(String[] args) {
        System.out.println(3/0);
    }

输出:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.exambner.exception.TextException.main(TextException.java:17)

 

posted @ 2022-12-17 05:46  Amireux-126  阅读(1008)  评论(0)    收藏  举报