Java 常见的运行时异常

    技术2025-12-12  18


    1.ArrayIndexOutOfBoundsException索引越界异常

    //数组越界异常java.lang.ArrayIndexOutOfBoundsException int[] arr = {1,2,3,4}; System.out.println(arr[0]); System.out.println(arr[4]);//java.lang.ArrayIndexOutOfBoundsException

    2.NullPointerException空指针异常

    直接输出没有问题,但是调用其功能方法就会报异常。

    //空指针异常 String name = null; System.out.println(name);//null System.out.println(name.length());//java.lang.NullPointerException

    3.ClassCastException类型转换异常

    String转Integer出错

    //类型转换异常 Object o = "abc"; Integer i = (Integer)o;//java.lang.ClassCastException

    4.ArithmeticException数学操作异常

    分母不能为0

    //数学操作异常 int a = 10/0; //java.lang.ArithmeticException: / by zero System.out.println(a);

    5.NumberFormatException数字转换异常

    //数字转换异常 String a = "10aa"; Integer integer = Integer.valueOf(a); System.out.println(integer);
    Processed: 0.022, SQL: 9