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); } }
IndexOutOfBoundsException
as a counter-example.
This exception does not provide information regarding the relevant indexes,
which would often be useful for solving the problem: