Effective Java 58 Use checked exceptions for recoverable conditions and runtime exceptions for programming errors
Three kinds of throwables
Throwables |
Checked |
Recoverable |
checked exceptions |
Y |
Y |
runtime exceptions |
N |
N |
errors |
N |
N |
Principle
- Use checked exceptions for conditions from which the caller can reasonably be expected to recover.
- Code that parses the string representation of an exception is likely to be non portable and fragile. Since the toString() implementation may change from release to release (Item 10).
- Provide methods that furnish information that could help the caller to recover.
For example, suppose a checked exception is thrown when an attempt to make a purchase with a gift card fails because the card doesn't have enough money left on it. The exception should provide an accessor method to query the amount of the shortfall, so the amount can be relayed to the shopper
- Use runtime exceptions to indicate programming errors. -The great majority of runtime exceptions indicate precondition violations. (e.g. ArrayIndexOutOfBoundException).
Note
All of the unchecked throwables you implement should subclass RuntimeException directly or indirectly.
Never use behaviorally identical to ordinary checked exceptions (which are subclasses of Exception but not RuntimeException).
Summary
Use checked exceptions for recoverable conditions and runtime exceptions for programming errors. Of course, the situation is not always black and white.
Effective Java 58 Use checked exceptions for recoverable conditions and runtime exceptions for programming errors的更多相关文章
- Effective Java Index
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...
- Effective Java 目录
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...
- 《Effective Java》读书笔记 - 9.异常
Chapter 9 Exceptions Item 57: Use exceptions only for exceptional conditions 这条item的意思就是,千万不要用except ...
- 【Effective Java】阅读
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...
- Effective Java 61 Throw exceptions appropriate to the abstraction
Exception translation: higher layers should catch lower-level exceptions and, in their place, throw ...
- Effective Java通俗理解(下)
Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...
- 《Effective Java(中文第二版)》【PDF】下载
<Effective Java(中文第二版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382186 Java(中文第二版)& ...
- Effective Java 第三版——70. 对可恢复条件使用检查异常,对编程错误使用运行时异常
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- Effective Java 第三版笔记(目录)
<Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将近8年的时 ...
随机推荐
- flex布局浅谈和实例
阿基米德曾说给我一个支点我可以撬动地球,而拥有flex基本可以撬动所有的布局. 1.flex布局基本介绍及效果展示 工欲善其事必先利其器,来来来,一起看下基础知识先(呵~,老掉牙,但是有用啊). ** ...
- Redis设计与实现-持久化篇
redis数据库 默认16个数据库,每个数据库由一个redis.h/redisDb结构表示,此结构里的dict字典与expires字典,其中dict保存了该库所有键值对,此字典即为键空间:expire ...
- ASP.NET MVC5利用EF,反向自动生成数据库
1.在Model类里面,写好相应的属性. using System; using System.Collections.Generic; using System.Linq; using System ...
- .net 中读取自定义Config文件
今天做一个windows插件式服务程序,插件有时要读取配置文件的设置,但是服务是动态加载到服务上的,没有办法作到动态修改服务的配置文件(app.config).在.net 2.0中有一个Configu ...
- 极简Unity调用Android方法
简介 之前写了篇unity和Android交互的教程,由于代码里面有些公司的代码,导致很多网友看不懂,并且确实有点小复杂,这里弄一个极简的版本 步骤 废话不多说,直接来步骤吧 1.创建工程,弄大概像这 ...
- C#递归题目代码
一列数的规则如下: 1.1.2.3.5.8.13.21.34...... 求第30位数是多少, 用递归算法实现. 代码: public class MainClass { public static ...
- Azure开发者任务之一:解决Azure Storage Emulator初始化失败
初学Windows Azure: 我打算开始学习Windows Azure.我安装了Azure SDK,然后在“Cloud”标签下选择Windows Azure模板,创建了一个项目,然后又创建了一个W ...
- 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar
[源码下载] 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...
- sql 两列相加存到另一列
假设表table1有a.b两个列,想生成另一个列为a列值+b列值计算列添加语句如下ALTER TABLE table1ADD c AS a+b
- [moka同学笔记]yii2.0表单的使用
1.创建model /biaodan.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/08/05 * Tim ...