Hi!
I always check the arguments of public functions and throw exceptions when something's wrong. (For private helpers I use assertions).
Like this:
if( a < 0 || a >= b )
throw new IllegalArgumentException("'a' must be greater or equal to 0 and
smaller than b ");
But it always annoys me to write these error messages. The message seems redundant to me, as the message is just the negation of the statement
a < 0 || a >= b
.
It also often happens that I rename the variable with refactoring (in eclipse) and then the message does not reflect the changes. Or I change the conditions and forget to change the messages.
It would be great, if I could write something like:
assertArgument(a >= 0 && a < b);
This should raise an IllegalArgumentException with a message like
"violated argument assertion: a >= 0 && a < b."
In C you could write a macro (actually in C assert is just a macro). Is there a simple way to do something like this in java?
Thank you!