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]非常小的下载图片脚本(非通用)

    说在最前面:这不是一个十分通用的下载图片脚本,只是根据我的一个小问题,为了减少我的重复性工作写的脚本. 问题 起因:我的这篇博文什么是真正的程序员浏览量超过了4000+. 问题来了:里面的图片我都是用 ...

  2. css中px,em和rem的区别

    css中px,em和rem的区别 今天,突然间发现一个特别有意思的问题,就是无意间看到一个网站中的em并不是16px,下面展开了对于px和em以及rem的探究. 首先,px是绝对长度单位,是相对于显示 ...

  3. BZOJ1087状压DP 解题报告

    1087: [SCOI2005]互不侵犯King Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的 ...

  4. 基于Lumisoft.NET组件的SMTP账号登陆检测

    在邮件处理的方面,Lumisoft.NET可以说是非常不错的一个选择,我在前面几篇文章中都介绍过这个组件. 基于Lumisoft.NET组件开发碰到乱码等一些问题的解决 基于Lumisoft.NET组 ...

  5. 详细讲解PHP中缓存技术的应用

    PHP,一门最近几年兴起的web设计脚本语言,由于它的强大和可伸缩性,近几年来得到长足的发展,php相比传统的asp网站,在速度上有绝对的优势,想mssql转6万条数据php如需要40秒,asp不下2 ...

  6. Python for循环内部实现的一个sample

    #!/usr/bin/env python # -*- coding: utf-8 -*- it = iter([1,2,3,4,5]) while True: try: x = next(it) p ...

  7. c#反射获取常量属性名以及其值(真正可用)

    最近因为要开发rpc平台的c#客户端,其中部分常量类为了自动加载的map,需要反射解析出静态常量,往上搜了一堆,都各种的不靠谱. 亲自研究了下,如下: Type t = typeof(SpiderEr ...

  8. Android5.0新特性——全新的动画(animation)

    全新的动画 在Material Design设计中,为用户与app交互反馈他们的动作行为和提供了视觉上的连贯性.Material主题为控件和Activity的过渡提供了一些默认的动画,在android ...

  9. 再谈visibility:hidden和display:none

    之前写过一篇有关visibility:hidden和display:none的文章:为什么要用用visibility:hidden;代替display:none;?主要是从浏览器性能方面入手,却没写两 ...

  10. Javascript面向对象编程(二)--- 构造函数的继承

    这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例 今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个"动 ...