异常(Exception)分几种类型?有什么区别?写出几个常见异常.
网友回答
【答案】 Exception分为两类:非运行是异常和运行时异常.\x0djava编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常.A:NullPointerException:对象的值是null\x0d举例:调用Person类的show方法\x0dPerson p = null;\x0dp.show();B:ClassCastException:类型转换异常\x0d举例:在多态中常见\x0dAnimal a = new Dog();\x0dCat c = (Cat)a;C:NoSuchElementException:没有这个元素异常\x0d举例:在迭代器中,已经访问到元素的末尾了,你还在继续访问.\x0dIterator it = array.iterator(); //只有两个元素\x0dSystem.out.println(it.next());\x0dSystem.out.println(it.next());\x0dSystem.out.println(it.next()); //NoSuchElementException\x0dD:IndexOutOfBoundsException:\x0d举例:指示某排序索引(例如对数组、字符串或向量的排序)超出范围时抛出.E:ArrayIndexOutOfBoundsException:数组索引越界异常\x0d举例:访问数组时,索引越界\x0dint[] arr = {1,2,3};\x0dSystem.out.println(arr[3]);F:NumberFormatException:数据格式化异常\x0d举例:把一个非数字字符串转换成数字类型\x0dint num = Integer.parseInt(abc);G:ClassNotFoundException:找不到类的异常\x0d举例:路径不对的时候.H:FileNotFoundException:找不到文件异常\x0d举例:在读取文件的时候,文件不存在.\x0dFileReader fr = new FileReader(fr.txt);I:ConcurrentModificationException:并发修改异常\x0d举例:在使用迭代器迭代数据的过程中,你又使用集合对象去操作元素.