zoukankan      html  css  js  c++  java
  • Effective Java 63 Include failure-capture information in detail message

    Principle

    To capture the failure, the detail message of an exception should contain the values of all parameters and fields that "contributed to the exception.

       

    One way to ensure that exceptions contain adequate failure-capture information in their detail messages is to require this information in their constructors instead of a string detail message.

       

    /**

    * Construct an IndexOutOfBoundsException.

    *

    * @param lowerBound the lowest legal index value.

    * @param upperBound the highest legal index value plus one.

    * @param index the actual index value.

    */

    public IndexOutOfBoundsException(int lowerBound, int upperBound, int index) {

    // Generate a detail message that captures the failure

    super("Lower bound: " + lowerBound +

    ", Upper bound: " + upperBound +

    ", Index: " + index);

    // Save failure information for programmatic access

    this.lowerBound = lowerBound;

    this.upperBound = upperBound;

    this.index = index;

    }

       

    As suggested in Item 58, it may be appropriate for an exception to provide accessor methods for its failure-capture information (lowerBound, upperBound, and inde xin the above example). It is more important to provide such accessor methods on checked exceptions than on unchecked exceptions, because the failure-capture information could be useful in recovering from the failure. It is rare (although not inconceivable) that a programmer might want programmatic access to the details of an unchecked exception. Even for unchecked exceptions, however, it seems advisable to provide these accessors on general principle (Item 10, page 53).

  • 相关阅读:
    数据库分区、分表、分库、分片
    C# 创建Windows Service(Windows服务)程序
    C# 自定义控件容器,设计时可添加控件
    redis配置文件中常用配置详解
    将博客搬至CSDN
    MD5加密之加密字符串
    MD5加密之提取文件的MD5特征码
    安卓手机下拉状态栏的代码实现
    Android中四大组件总结
    Android中内容提供者ContentProvider的详解
  • 原文地址:https://www.cnblogs.com/haokaibo/p/include-failure-capture-information-in-detail-message.html
Copyright © 2011-2022 走看看