Effective Java 57 Use exceptions only for exceptional conditions
Principle
- Exceptions are, as their name implies, to be used only for exceptional conditions; they should never be used for ordinary control flow.
// Horrible abuse of exceptions. Don't ever do this!
try {
int i = 0;
while(true)
range[i++].climb();
} catch(ArrayIndexOutOfBoundsException e) {
}
- A well-designed API must not force its clients to use exceptions for ordinary control flow.
for (Iterator<Foo> i = collection.iterator(); i.hasNext(); ) {
Foo foo = i.next();
...
}
If Iterator lacked the hasNext method, clients would be forced to do this instead:
// Do not use this hideous code for iteration over a collection!
try {
Iterator<Foo> i = collection.iterator();
while(true) {
Foo foo = i.next();
...
}
} catch (NoSuchElementException e) {
}
- An alternative to providing a separate state-testing method is to have the state dependent method return a distinguished value such as null if it is invoked with the object in an inappropriate state. This technique would not be appropriate for Iterator, as null is a legitimate return value for the next method.
- Guidelines to choose between a state-testing method and a distinguished return value.
|
Different concerns |
State-testing method |
Distinguished value |
|
Concurrent Object Accessing without external synchronization/externally induced state transitions |
N |
Y |
|
Performance concerns |
N |
Y |
|
Other situations except the conditions above |
Y |
N |
|
Readability |
Y |
N |
|
Incorrect use detectable |
Y |
N |
Summary
Exceptions are designed for use in exceptional conditions. Don't use them for ordinary control flow, and don't write APIs that force others to do so.
Effective Java 57 Use exceptions only for exceptional conditions的更多相关文章
- 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 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》读书笔记 - 9.异常
Chapter 9 Exceptions Item 57: Use exceptions only for exceptional conditions 这条item的意思就是,千万不要用except ...
- Effective Java 目录
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...
- Effective Java 65 Don't ignore exceptions
Principle An empty catch block defeats the purpose of exceptions, which is to force you to handle ex ...
- 【Effective Java】阅读
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...
- Effective Java通俗理解(下)
Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...
- Effective Java 第三版——7. 消除过期的对象引用
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- 《Effective Java(中文第二版)》【PDF】下载
<Effective Java(中文第二版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382186 Java(中文第二版)& ...
随机推荐
- datagrid动态数据添加超链接的方法
首先,我我们要有一个json格式的datagrid_data.json文件,如下:
- 不可或缺 Windows Native (6) - C 语言: 函数
[源码下载] 不可或缺 Windows Native (6) - C 语言: 函数 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 函数 示例cFunction.h # ...
- php实现添加图片水印
实际运行时需要开启php 的gd2功能,运行环境php4.0以上(demo中的路径改为实际路径) <?php/*打开图片*/ //1.配置图片路径 $src="image/61.jpg ...
- 机器学习实战 - 读书笔记(13) - 利用PCA来简化数据
前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习心得,这次是第13章 - 利用PCA来简化数据. 这里介绍,机器学习中的降维技术,可简化样品数据. ...
- 正态QQ图的原理
code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...
- ahjesus SSHkey登陆linux服务器,无需密码,ubuntu
cd ~/.ssh/如果目录不存在就新建一个 mkdir ~/.ssh 制作公匙 ssh-keygen -t rsa默认会生成id_rsa.pub的公匙 将公匙推送到指定的服务器 scp id_rsa ...
- Visual Studio 2013 Preview 新功能
先来看一下Visual Studio的版本历史: 1. Visual Studio.NET 2002 2. Visual Studio.NET 2003 3. Visual Studio.NET 20 ...
- ajax跨子域请求的两种现代方法
因为面向互联网的性质,我们公司的大部分系统都采用多子域的方式进行开发和部署,以达到松耦合和分布式的目的,因此子系统间的交互不可避免.虽然通过后台的rpc框架解决了大部分的交互问题,但有些情况下,前端直 ...
- For循环语句的使用
一.For循环语句 说明:For循环用于循环次数已经确定的情况下. 格式:for(循环变量赋初值; 循环条件; 循环变量增值) { ·····语句 } 举例:求 ...
- Sharepoint 2013 关于"SPChange"简介
在SharePoint中,我们经常会需要获取那些改变的项目,其实api为我们提供了SPChange对象,下面,我们通过列表简单介绍下这一对象. 1.创建一个测试列表,名字叫做“SPChangeItem ...