`
leonzhx
  • 浏览: 767829 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 58: Use checked exceptions for recoverable conditions and runtime exception

阅读更多

1.  The Java programming language provides three kinds of throwables: checked exceptions, runtime exceptions, and errors.

 

2.  Use checked exceptions for conditions from which the caller can reasonably be expected to recover.

 

3.  Each checked exception that a method is declared to throw is therefore a potent indication to the API user that the associated condition is a possible outcome of invoking the method.

 

4.  If a program throws an unchecked exception or an error, it is generally the case that recovery is impossible and continued execution would do more harm than good. Use runtime exceptions to indicate programming errors.

 

5.  The great majority of runtime exceptions indicate precondition violations. A precondition violation is simply a failure by the client of an API to adhere to the contract established by the API specification.

 

6.  There is a strong convention that errors are reserved for use by the JVM to indicate resource deficiencies, invariant failures, or other conditions that make it impossible to continue execution. All of the unchecked throwables you implement should subclass RuntimeException (directly or indirectly).

 

7.  Use checked exceptions for recoverable conditions and runtime exceptions for programming errors.

 

8.  If you believe a condition is likely to allow for recovery, use a checked exception; if not, use a runtime exception. If it isn’t clear whether recovery is possible, you’re probably better off using an unchecked exception.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics