java常见的异常类型

Exception分为两类:非运行是异常和运行时异常。
java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常。A:NullPointerException:对象的值是null
举例:调用Person类的show方法
Person p = null;
p.show();B:ClassCastException:类型转换异常
举例:在多态中常见
Animal a = new Dog();
Cat c = (Cat)a;C:NoSuchElementException:没有这个元素异常
举例:在迭代器中,已经访问到元素的末尾了,你还在继续访问。
Iterator it = array.iterator(); //只有两个元素
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next()); //NoSuchElementException
D:IndexOutOfBoundsException:
举例:指示某排序索引(例如对数组、字符串或向量的排序)超出范围时抛出。E:ArrayIndexOutOfBoundsException:数组索引越界异常
举例:访问数组时,索引越界
int[] arr = {1,2,3};
System.out.println(arr[3]);F:NumberFormatException:数据格式化异常
举例:把一个非数字字符串转换成数字类型
int num = Integer.parseInt("abc");G:ClassNotFoundException:找不到类的异常
举例:路径不对的时候。H:FileNotFoundException:找不到文件异常
举例:在读取文件的时候,文件不存在。
FileReader fr = new FileReader("fr.txt");I:ConcurrentModificationException:并发修改异常
举例:在使用迭代器迭代数据的过程中,你又使用集合对象去操作元素。
posted @ 2016-10-08 22:59  九命黑猫  阅读(234)  评论(0编辑  收藏  举报