Java中常见的几种RuntimeException

请你说出Java中几种常见的RuntimeException?这是面试过程中经常被问到的问题,业余时间做了简单总结,希望能对大家有所帮助,如有不足希望指出。

1. NullPointerException (空指针引用异常)

String str = null; str.toString(); 

Exception in thread "main" java.lang.NullPointerException

2.ClassCastException(类型强制转换异常)

Object str = "abc"; Integer integer =(Integer)str;

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

3. IllegalArgumentException(传递非法参数异常)

System.out.println(Timestamp.valueOf("2019/02/15 00:00:00"));

Exception in thread "main" java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

4.ArithmeticException ( 算术运算异常)

System.out.println(2/0); 

Exception in thread "main" java.lang.ArithmeticException: / by zero

5. ArrayIndexOutOfBoundsException (数组下标越界异常)

String[] strings = {"a"}; 
System.out.println(strings[1]); 

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

6. NegativeArraySizeException(数组负下标异常)

String[] strings = new String[-1]; 
Exception in thread "main" java.lang.NegativeArraySizeException

7. NumberFormatException(数字格式异常)

Integer integer = Integer.parseInt("o"); 

Exception in thread "main" java.lang.NumberFormatException: For input string: "o"

8. ArrayStoreException(数组存储异常)

Object[] strings = new String[2]; strings[0]=0;

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer

9. IndexOutOfBoundsException (下标越界异常)

List list = new ArrayList(); list.get(0); 

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

这里就先简单介绍这几种,想详细了解的同学可以自行翻看源码。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值