Three kinds of throwables

Throwables

Checked

Recoverable

checked exceptions

Y

Y

runtime exceptions

N

N

errors

N

N

Principle

  1. Use checked exceptions for conditions from which the caller can reasonably be expected to recover.
  2. 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).
  3. 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

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

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

  2. Effective Java 目录

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

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

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

  4. 【Effective Java】阅读

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

  5. Effective Java 61 Throw exceptions appropriate to the abstraction

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

  6. Effective Java通俗理解(下)

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

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

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

  8. Effective Java 第三版——70. 对可恢复条件使用检查异常,对编程错误使用运行时异常

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  9. Effective Java 第三版笔记(目录)

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

随机推荐

  1. ActiveMQ学习(四)——应用程序接口

    在 Java 里有 JMS的多个实现.其中 apache 下的 ActiveMQ就是不错的选择. 用 ActiveMQ最好还是了解下 JMS JMS 公共 点对点域 发布/订阅域 Connection ...

  2. 0422 发现( 数学口袋精灵)bug

    团队博客地址: 甘佳萍:http://www.cnblogs.com/gjpg/ 李鹏飞:http://www.cnblogs.com/l549023320/ 赵创佳:http://www.cnblo ...

  3. C#中override和new修饰符的区别

    (new)“隐藏”,(override)“覆盖”(重写).不过要弄清楚这两个有什么区别确实也很难,因为子类在使用父类方法时根本看不出区别,子类不管父类是new了还是override了,用的都是父类方法 ...

  4. java俄罗斯方块游戏代码

    java俄罗斯方块游戏代码: package com; import java.awt.Color; import java.awt.Graphics; import java.awt.event.K ...

  5. 随笔分类 - 无废话ExtJs系列教程

    随笔分类 - 无废话ExtJs系列教程 摘自:http://www.cnblogs.com/iamlilinfeng/category/385121.html ExtJs 入门教程 摘要: extjs ...

  6. windbg学习进阶之——dump分析常用命令收集

    #重要说明 (1) windbg命令分为标准命令,元命令和扩展命令. 标准命令提供最基本的调试功能,不区分大小写.如:bp  g  dt  dv  k等 元命令提供标准命令没有提供的功能,也内建在调试 ...

  7. 通过发布项目到IIS上,登录访问报系统找不到System.Web.Mvc

    我发布项目到IIs,通过IIS的端口来访问直接下面的错误

  8. MyBatis插入语句返回主键值

    插入语句xml代码: <insert id="insertUser" parameterType="com.spring.mybatis.po.User" ...

  9. Autofac全面解析系列(版本:3.5) – [使用篇(推荐篇):2.解析获取]

    前言 Autofac是一套高效的依赖注入框架. Autofac官方网站:http://autofac.org/ Autofac在Github上的开源项目:https://github.com/auto ...

  10. 初识HTTP协议

    本篇文章从概念上初识HTTP协议,参考链接:http://www.runoob.com/http/http-tutorial.html 目录: 一.HTTP协议    HTTP 工作原理    HTT ...