黑马程序员--Java基础常见异常整理

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流!


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的情况,该引用还在用于操作实体。
Demo1:
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


Demo2:
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
}
}

Demo3:
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 playGame()
{
System.out.prinltn("小猫咪玩游戏");
}
}
class Dog extends Animal
{
public void show()
{
System.out.prinltn("show dog");
}
public void playGame()
{
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.playGame();


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"这个元素,如果有,就再添加一个元素:"IOS"
Iterator it = list.iterator();
while (it.hasNext()) {
String s = (String) it.next();
if ("hello".equals(s)) {
list.add("IOS");//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("IOS");
}
}
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("IOS");
}
}
System.out.println("list:" + list);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值