`
leonzhx
  • 浏览: 793442 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 58: Use checked exceptions for recoverable conditions and runtime exception

阅读更多

1.  The Java programming language provides three kinds of throwables: checked exceptions, runtime exceptions, and errors.

 

2.  Use checked exceptions for conditions from which the caller can reasonably be expected to recover.

 

3.  Each checked exception that a method is declared to throw is therefore a potent indication to the API user that the associated condition is a possible outcome of invoking the method.

 

4.  If a program throws an unchecked exception or an error, it is generally the case that recovery is impossible and continued execution would do more harm than good. Use runtime exceptions to indicate programming errors.

 

5.  The great majority of runtime exceptions indicate precondition violations. A precondition violation is simply a failure by the client of an API to adhere to the contract established by the API specification.

 

6.  There is a strong convention that errors are reserved for use by the JVM to indicate resource deficiencies, invariant failures, or other conditions that make it impossible to continue execution. All of the unchecked throwables you implement should subclass RuntimeException (directly or indirectly).

 

7.  Use checked exceptions for recoverable conditions and runtime exceptions for programming errors.

 

8.  If you believe a condition is likely to allow for recovery, use a checked exception; if not, use a runtime exception. If it isn’t clear whether recovery is possible, you’re probably better off using an unchecked exception.

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 70: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors Item 71: Avoid unnecessary use of checked exceptions Item 72: Favor the use of standard ...

    详解Java中Checked Exception与Runtime Exception 的区别

    Java 中 Checked Exception 与 Runtime Exception 的区别 Java 中的异常处理机制是 Java 语言的一个重要特色,它允许程序产生例外状况。在学习 Java 时,我们需要了解不同种类的异常的区别。Java 提供了两种异常...

    Checked and Unchecked Exception

    异常分为两种类型:已检查异常(Checked Exception)和未检查异常(Unchecked Exception)。 **已检查异常(Checked Exception)** 已检查异常是那些在编译时需要处理的异常。Java强制程序员要么在方法签名中声明...

    Exceptions in Java and Eiffel:Two Extremes in Exception Design and Application

    Java中的异常分为两大类:检查异常(checked exceptions)和运行时异常(runtime exceptions)。检查异常是指编译器强制要求程序员必须处理的异常类型,通常用于表示程序中可能出现的正常但无法预料的情况;而运行时...

    EurekaLog_7.5.0.0_Enterprise

    17)..Added: Support for relative file paths and environment variables for events and various module paths 18)..Added: Logging in Manage tool 19)..Added: Windows 10 version detection 20)..Added: Stack ...

    jQuery中:checked选择器用法实例

    `:checked`选择器的基本语法非常简单,就像这样: ```javascript $(":checked") ``` 这行代码会返回页面上所有当前被选中的复选框和单选按钮的jQuery对象集合。你可以进一步对这些元素进行各种操作,例如改变它们的...

    QT CheckBox复选框状态设置、信号绑定, setCheckState、checkState、setTristate

    它接受一个`Qt::CheckState`枚举值作为参数,该枚举定义了三种状态:`Qt::Unchecked`(未选中),`Qt::Checked`(选中),以及`Qt::PartiallyChecked`(三态,通常用于表示不确定状态)。例如,如果你想将一个`CheckBox`...

    exception handling for c++

    This paper outlines a design for an exception handling mechanism for C++. It presents the reasoning behind the major design decisions and considers their implications for implementation alternatives. ...

    C++ and the Perils of Double Checked Locking.zip

    《C++ and the Perils of Double Checked Locking》是一篇探讨C++编程中双重检查锁定(Double-Checked Locking)模式潜在问题的文献。在多线程编程中,双重检查锁定是一种常见的优化策略,旨在减少对同步原语的依赖...

    Java精华(免费版)

     从逻辑的角度来说,checked exceptions和runtime exception是有不同的使用目的的。checked exception用来指示一种调用方能够直接处理的异常情况。而runtime exception则用来指示一种调用方本身无法处理或恢复...

    C++ and the Perils of Double-Checked Locking

    在介绍双检锁模式(Double-Checked Locking Pattern,DCLP)的C++实现中,Scott Meyers和Andrei Alexandrescu在其2004年的文章中指出,传统的单例模式实现并不具备线程安全性。单例模式是设计模式中经常被提及的一种...

    Checked Exception(受检的异常)2---马克-to-win java视频

    Checked Exception(受检的异常)2---马克-to-win java视频

    TMS Pack for FireMonkey2.3.0.1

    Built-in support for LiveBindings in TTMSFMXTableView and TTMSFMXTileList, allows to bind any item element to data Includes various demos and an extensive PDF developers guide Includes various helper ...

    移除旧版intraweb工具,支持RDS 2009~XE6

    Search and list files but DON'T delete them: If checked, will list all IntraWeb files found on Delphi installation path. No files are deleted. Probably is a good idea to check that before actually ...

    Java异常处理机制应用研究.pdf

    Exception 又可以分为两种:Checked Exception 和 Runtime Exception。Checked Exception 是编译器检查的异常,例如 IOException、SQLException 等。Runtime Exception 是运行时异常,例如 NullPointerException、...

    Professional C# 3rd Edition

    The checked and unchecked Operators 134 The is Operator 135 The as Operator 136 The sizeof Operator 136 The typeof Operator 136 Contents Operator Precedence 137 Type Safety 137 Type Conversions 138 ...

    QTableWidget中嵌入多个复选框

    QTableWidgetItem *checkBoxItem = new QTableWidgetItem(QTableWidgetItem::Type); ``` 2. **设置复选框**:将创建的`QTableWidgetItem`插入到`QTableWidget`中,指定行和列位置。然后,可以使用`setCheckState()`...

    QT的QTreeWidget有checkbox时轻松选中

    item->setCheckState(checkboxColumn, item->checkState(checkboxColumn) == Qt::Checked ? Qt::Unchecked : Qt::Checked); } else { // 如果点击的是其他列,也切换复选框状态 for (int i = 0; i < item->...

Global site tag (gtag.js) - Google Analytics