Item 75: Include failure-capture information in detail messages
75. Include error log information in detailed messages
- Include information useful for error analysis in the exception detail message (the one that appears in the stack trace). For example, in case of IndexOutOfBoundsException, the lower limit value, upper limit value, and index value should be included. (Actually, as of Java 9, there is a constructor that outputs only the index value as shown below)
/**
* Constructs a new {@code IndexOutOfBoundsException} class with an
* argument indicating the illegal index.
*
* <p>The index is included in this exception's detail message. The
* exact presentation format of the detail message is unspecified.
*
* @param index the illegal index.
* @since 9
*/
public IndexOutOfBoundsException(int index) {
super("Index out of range: " + index);
}
- Do not include passwords, encryption keys, etc. in detailed messages, as stack traces are visible to many people.