Exceptions In Java

Java中的异常分为两种类型:checked Exception和unchecked Exception。

 Why did the designers decide to force a method to specify all uncaught checked exceptions that can be thrown within its scope? Any Exception that can be thrown by a method is part of the method's public programming interface. Those who call a method must know about the exceptions that a method can throw so that they can decide what to do about them. These exceptions are as much a part of that method's programming interface as its parameters and return value.

一个方法没有返回期望值的原因有两个:一个是contingency(应变),一个是fault(故障)。

一个contingency应该被程序设计者考虑到,并把它作为程序的正常行为之一,它对影到checked exception对于一个方法而言,它应该把contingency的情况大包为checked exception抛出;如果是库类的方法,一定要设计好,因为方法总会有些不太常出现,但是还是会可能的返回情况,这就刚好用checked exception来表达,当应用程序调用该方法的时候,会根据不同的情况对其进行分别处理,从而返回给最终使用者一个正确的值。抛出这种异常,应该完全不影响程序的正常运行

一个fault是设计者认为不会发生的,它对应到unchecked exception。

应变和故障的对比
条件应变故障
被认为是设计的一部分一个糟糕的意外
预计会发生经常发生不会发生
关注方上游调用需要修好找个问题的人
价值另外一种返回方式程序bug,硬件系统故障,配置错误,丢失的文件,服务器没有运行
与异常匹配checkedunchecked

 

参考资料:

Effective Java Exceptions

http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值