zoukankan      html  css  js  c++  java
  • Effective Java 58 Use checked exceptions for recoverable conditions and runtime exceptions for programming errors

    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.  

  • 相关阅读:
    vcsa7.0可能不兼容esxi6.7
    使用livecd 更改root密码
    虚拟机初始化脚本
    一句话修改UUID
    vcsa 6.7 u1升级6.7 u2--6.7U3---6.7U3c
    【转载】:FreeRadius安装及与openldap的连接(centos 7 环境)
    freeradius 关联LDAP认证-按属性过滤LDAP目录中的用户
    使用包ldap3进行Python的LDAP操作
    2020年1月29日-学习flask第一天
    Python-xlwt库的基本使用
  • 原文地址:https://www.cnblogs.com/haokaibo/p/use-checked-exceptions-for-recoverable-conditions-and-runtime-exceptions-for-programming-errors.html
Copyright © 2011-2022 走看看