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. 转载:混淆包含SlidingMenu、gson等Android代码的proguard写法

    描述:作为一个写了几年c#的码农,咱的java水平实在不敢恭维,但是最近有个任务需要弄点java的代码,还要研究混淆.网上百度并实验了整整一天,终于算是有点成果了.其中混淆的时候由于用到了gson包, ...

  2. 新手学习Cocoapods教程

    CocoaPods简介 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如AFNetWorking等等.手动去下载所需类库十分麻烦.另外一种常见情况是,你项目中用到的类库有更新,你必须得重新下 ...

  3. SCRUM项目 4.0

    4.0----------------------------------------------- 1.准备看板. 形式参考图4. 2.任务认领,并把认领人标注在看板上的任务标签上. 先由个人主动领 ...

  4. JavaScript基础概念

    1.JavaScript在浏览器中是解释执行的: 2.JavaScript是一中弱类型的语言,在使用变量前,可以不用先申明: 3.JavaScript使用了对象对象程序设计思想: 4.JavaScri ...

  5. ViewData和TempData以及Session的小结

    ViewData一般用在从控制器向页面上传递数据. Public ActionResult Show() { ViewData["message"]="你好"; ...

  6. DataTable 获取列名 DataTable批量更新至数据库

    好久没写东西了,这几个月也没下功夫钻研技术,愧疚啊.说下最近刚学会的DataTable 的用法吧,新手适合看下. 1 DataTable 获取列名 在处理数据的时候大家都会用到模型,从datatabl ...

  7. SVG操作插件:SVG.JS 个人提取部分实用中文文档

    先贴出github地址:https://github.com/svgdotjs/svg.js(也就是原文档的说明和文件的下载地址) 创建SVG文档 var draw = SVG('drawing'). ...

  8. 【AngularJS学习笔记】00 序

    AngularJS通过新的属性与表达式来扩展HTML,有一种很形象的叫法,定义它为声明式语言. 为克服HTML在构建应用上的不足而设计! 这是它的目标. 它的官网进不去,应该是被墙了,这是goegle ...

  9. asp.net 发送邮件

    asp.net 发送邮件 System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();            msg.To. ...

  10. 度娘果然毫无节操,纯粹就是order by 广告费 desc

    度娘果然毫无节操,纯粹就是order by 广告费 desc 必应搜索出来排第一,度娘根本就找不到在哪....