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. [Architect] Abp 框架原理解析(2) EventBus

    本节目录 原理介绍 Abp源码分析 代码实现 原理介绍 事件总线大致原理: (1)       在事件总线内部维护着一个事件与事件处理程序相映射的字典. (2)       利用反射,事件总线会将实现 ...

  2. Gradle学习系列之五——自定义Property

    在本系列的上篇文章中,我们讲到了增量式构建,在本篇文章中,我们将讲到如何自定义Project的Property. 请通过以下方式下载本系列文章的Github示例代码: git clone https: ...

  3. C#实现的18位身份证格式验证算法

    18位身份证标准在国家质量技术监督局于1999年7月1日实施的GB11643-1999<公民身份号码>中做了明确的规定. GB11643-1999<公民身份号码>为GB1164 ...

  4. sql server 2008还原数据库,出现缺少介质问题

    我在sql server2008中备份数据库时,新增了一个自己建立的数据库,备份成功后,在去别的电脑总是还原数据 还原不了,最后在网上找到了解决方案

  5. 启动Eclipse 弹出"Failed to load the JNI shared library jvm.dll"错误

    启动Eclipse 弹出"Failed to load the JNI shared library jvm.dll"错误,如下 原因:eclipse的版本与jre或者jdk版本不 ...

  6. php中设定一个全局异常处理。全局catch。默认catch。默认异常处理

    <?php function handleMissedException($e) { echo "Sorry, something is wrong. Please try again ...

  7. NYOJ:题目860 又见01背包

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=860 方法一:不用滚动数组(方法二为用滚动数组,为方法一的简化) 动态规划分析:最少要拿总 ...

  8. 利用多写Redis实现分布式锁原理与实现分析(转)

    利用多写Redis实现分布式锁原理与实现分析   一.关于分布式锁 关于分布式锁,可能绝大部分人都会或多或少涉及到. 我举二个例子:场景一:从前端界面发起一笔支付请求,如果前端没有做防重处理,那么可能 ...

  9. 【GOF23设计模式】模板方法模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_模板方法模式.钩子函数.方法回调.好莱坞原则 package com.test.templateMethod; publi ...

  10. 对CSS进行wxss思路学习,display属性。

    先来概要一下学习思路: 本系列内容,将针对微信小程序中的WXSS学习,所以在学习CSS时每一个知识点都在小程序IDE中进行实践,达到最好的学习效果. 由于wxss与CSS有些许不同,在学习CSS过程中 ...