RuntimeException为啥不用声明抛出?

异常,错误都是同一种父类:java.lang.Throwable

IllegalStateException属于RuntimeException,RuntimeException又是Exception的子类

RuntimeException的特点是非受检异常,区别于受检异常,也就是java系统允许可以不catch(当然也可以人为catch,比如特殊业务逻辑需要),在运行时碰到就抛出

关键RuntimeException为什么会被系统允许不被catch...
也就是可能会抛出RuntimeException的方法不被要求在方法throws clause中声明。

原因我想RuntimeException相对那些受检异常有更多的不确定性和普遍性,如果改为受检异常,代价较大,也没必要,因为一般它总不为业务逻辑所容(受检异常就容?也不容!是不是RuntimeException更不为所容?因此不catch直接抛出导致中断程序?也可能中不中断都不是重点,实现代价才是重点)。

对比几个可能会有更好的实感:
受检异常:ClassNotFoundException, SQLException, NoSuchMethodException...
RuntimeException:NullPointerException, IllegalArgumentException, ArithmeticException

 

http://bbs.csdn.net/topics/390279427?page=1#post-392900808

 

RuntimeExceptions are thrown for things that were preventable.


Exceptions are thrown for things that were unpreventable but recoverable


Errors are thrown for things that were unpreventable and unrecoverable.

 

For example, NullPointerExceptions can always be avoided and are therefore unchecked exceptions. A RemoteException could occur when there is a network failure, which cannot be reasonably prevented before the method call and therefore is checked.

 

http://stackoverflow.com/questions/2884836/why-nullpointerexception-is-a-runtime-exception-and-remoteexception-not

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值