Pass all pertinent data to exceptions

When an exception occurs, it's important that all pertinent data be passed to the exception's constructor. Such data is often critical for understanding and solving the problem, and can greatly reduce the time needed to find a solution.

The this reference is sometimes useful for this purpose, since toString is implicitly called. In addition, if you are defining exception classes yourself, you may even design your constructors to force the caller to pass the pertinent data.

Uninformative stack traces are very frustrating for the maintainer, and have been known to inspire unpleasant cursing.

Example

public final class RangeChecker {

  /**
  * Return <tt>true</tt> only if <tt>number</tt> is in the range
  * <tt>low..high</tt> (inclusive).
  *
  * @param <tt>low</tt> less than or equal to <tt>high</tt>.
  */
  static public boolean isInRange(int number, int low, int high){
    if (low > high) {
      throw new IllegalArgumentException("Low:" + low + " greater than High:" + high);
    }
    return (low <= number && number <= high);
  }
} 

Effective Java mentions IndexOutOfBoundsException as a counter-example. This exception does not provide information regarding the relevant indexes, which would often be useful for solving the problem: