Principle

  1. 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) {

    }

  2. 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) {

    }

  3. 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.
  4. 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的更多相关文章

  1. Effective Java 61 Throw exceptions appropriate to the abstraction

    Exception translation: higher layers should catch lower-level exceptions and, in their place, throw ...

  2. 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 ...

  3. 《Effective Java》读书笔记 - 9.异常

    Chapter 9 Exceptions Item 57: Use exceptions only for exceptional conditions 这条item的意思就是,千万不要用except ...

  4. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  5. 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 ...

  6. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  7. Effective Java通俗理解(下)

    Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...

  8. Effective Java 第三版——7. 消除过期的对象引用

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  9. 《Effective Java(中文第二版)》【PDF】下载

    <Effective Java(中文第二版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382186 Java(中文第二版)& ...

随机推荐

  1. [python基础知识]python内置函数map/reduce/filter

    python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...

  2. C++ 封装互斥对象

    多线程程序中为了防止线程并发造成的竞态,需要经常使用到Mutex进行数据保护.posix提供了phtread_mutex_t进行互斥保护数据.Mutex的使用需要初始化和释放对应(phtread_mu ...

  3. JS魔法堂:属性、特性,傻傻分不清楚

    一.前言 或许你和我一样都曾经被下面的代码所困扰 var el = document.getElementById('dummy'); el.hello = "test"; con ...

  4. vs.net_2003 下载 虽然是老古董了,但还是很有用的。

    系统要求 支持的操作系统: Windows 2000; Windows NT; Windows Server 2003; Windows XP 以下VS2003的下载链接: http://bcsoft ...

  5. IEnumerable接口

    IEnumerable接口顾名思义就是 可枚举的,可列举的. 接口也很简单,返回一个 枚举器对象 IEnumerator . [ComVisible(true), Guid("496B0AB ...

  6. 【循序渐进学Python】11.常用标准库

    安装完Python之后,我们也同时获得了强大的Python标准库,通过使用这些标准库可以为我们节省大量的时间.这里是一些常用标准库的简单说明.更多的标准库的说明,可以参考Python文档 sys 模块 ...

  7. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\helloiis\ceb8cab3\4db603d8\App_global.asax.gr73hi-k.dll”--“拒绝访问。 ”

    我的报错页面: 我是使用的第一种方法解决的. 转至http://blog.csdn.net/zyzlywq/article/details/17916799 解决方法: 1,通常的解决方法:原因是由于 ...

  8. 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件

    [源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...

  9. css:条件注释判断浏览器

    所有的IE可识别 Target ALL VERSIONS of IE <!--[if IE]> <link rel="stylesheet" type=" ...

  10. sql:MySQL 6.7 表,视图,存储过程结构查询

    #数据库MySQL 6.7 use sakila; #查询表名 show tables; # SELECT TABLE_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA. ...