您的位置:首页 > 编程语言 > Java开发

java常见异常归纳

2014-11-14 11:16 260 查看

Java常见异常归纳

JAVA程序编译时,由于非法操作和访问,经常会出现异常提示。一下总结了常见的异常..

1.ArrayIndexOutOfBoundsException:数组角标越界异常:操作数组时访问到了数组中不存在的角标

int[] arr  = new int[5];

System.out.println(arr[5]);//Exception in thread "main" java.lang.ClassCastException: Cat cannot be cast to Dog

  //数组索引范围为0~arr.length-1

2.NullPointerException:空指针异常:当引用没有任何指向值为null的情况,该引用还在用于操作实体。

ExceptionDemo1:

int[] arr  = new int[5];

arr[0] = 5;

System.out.println(arr[0]);

arr = null;

System.out.println(arr[0]);//Exception in thread "main" java.lang.NullPointerException

ExceptionDemo2:

class Person

{
void say()
{
System.out.println("我是一个人");
}

}

class  Demo

{
public static void main(String[] args) 
{
Person p = new Person();
p.say();
p = null;
p.say();//Exception in thread "main" java.lang.NullPointerException
}

}

ExceptionDemo3:

Collection c = new ArrayList();

c.add("hello");

c.add("null");

c.add("java");

//面向过程判断"java"是否存在

Object[] strs = c.toArray();

for(int i = 0; i<strs.length; i++)

{
String s = (String)strs[i];
if(s.equals("java"))

{    //容易出现空指针异常,改为if("java".equals(s))
sop("java");
}

}

3.ClassCastException 类型转换异常:

Demo:多态中

class Animal

{
public void show()
{
System.out.prinltn("show animal");
}

}

class Cat extends Animal

{
public void show()
{
System.out.prinltn("show cat");
}
public void play()
{
System.out.prinltn("猫玩");
}

}

class Dog extends Animal

{
public void show()
{
System.out.prinltn("show dog");
}
public void play()
{
System.out.prinltn("狗玩");
}

}

class  DuoTaiDemo2

{
public static void main(String[] args) 
{
//多态
Animal a = new Animal();//向上转型
a.show();
a = new Cat();//向上转型
Cat c = (Cat)a;//向下转型
c.show();
c.play();
Dog d = (Dog)a;//Exception in thread "main" java.lang.ClassCastException: Cat cannot be cast to Dog
}

}

4.NoSuchElementException 没有这样的元素异常:

// 创建对象

Collection c = new ArrayList();

// 添加元素

c.add("hello");

c.add("world");

c.add("java");

Iterator it = c.iterator();// 这是返回的是Iterator的子类对象,多态

Object obj = it.next();

System.out.println(obj);//hello

System.out.println(it.next());//world

System.out.println(it.next());//java

System.out.println(it.next());//NoSuchElementException 一共只有三个元素

System.out.println(it.next());

4.IndexOutOfBoundsException 索引越界异常:

List list = new ArrayList();

list.add("hello");

list.add("world");

list.add("java");

//List特有功能

//添加

list.add(1,"javaSE");

list.add(3,"javaEE");

//list.add(7,"javaEE");//IndexOutOfBoundsException

    System.out.println(list);//[hello, javaSE, world, javaEE, java]

//删除

    list.remove(6);//IndexOutOfBoundsException Index:6,Size:5

    //获取

    list.get(6);//IndexOutOfBoundsException: Index: 6, Size: 5

5.ConCurrentModificationException 并发修改异常:

// 创建集合对象

List list = new ArrayList();

// 添加元素

list.add("hello");

list.add("world");

list.add("java");

// 需求:请遍历集合,判断其中是否有"hello"这个元素,如果有,就再添加一个元素:"JAVA"

Iterator it = list.iterator();

while (it.hasNext()) {

String s = (String) it.next();

if ("hello".equals(s)) {

list.add("JAVA");//ConCurrentModificationException 通过迭代器遍历集合时,不能通过集合去操作。

//有两个解决办法 A:ListIterator B:for

//it = list.iterator();// 这样从原理是可以解决的,但是它会引起另外一个问题。OutOfMemoryError

}

}

System.out.println("list:" + list);

解决办法:

// 完全通过集合实现

for (int x = 0; x < list.size(); x++) {

String s = (String) list.get(x);

if ("hello".equals(s)) {

list.add("JAVA");

}

}

System.out.println("list:"+list);

System.out.println("-----------");

// 遍历

ListIterator lit = list.listIterator();

while (lit.hasNext()) {

String s = (String) lit.next();

if ("hello".equals(s)) {

lit.add("JAVA");

}

}

System.out.println("list:" + list);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: