常见的RuntimeException异常

 

1.NullPointerException  空指针异常  

空指针异常非常常见,在业实际务场景中经常出现

String ss=null;
ss.equals("str");

 

2.ArrayIndexOutOfBoundsException 数组索引越界异常

ArrayList中只有1个数据,最大索引是0,当获取下标为1的数时,就会ArrayIndexOutOfBoundsException 

ArrayList arrayList = new ArrayList();
arrayList.add(1);

arrayList.get(1);

或者

int []arr = new int[3];
System.out.println(arr[3]);

 

3.ArithmeticException 数学计算异常

比如 0做分母时,会提示ArithmeticException: / by zero

int a = 1/0;

 

4.NumberFormatException 字符串转换为数字异常

将非数字字符串转换成非数字时

String str = "#";
Integer.valueOf(str);

 

5.ClassCastException 类型强制转换异常

将object类强转为Integer时会报此异常(强转只能向上)

Object object = new Object();
Integer integer = (Integer)object;

将Integer类型转换成String时

Object y = new Integer(0);
System.out.println((String)y);

 

其他的runtimeException 可以去java源码中找 寻找方法是看该类是否继承了 RuntimeException

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值