commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilles Sadowski <>
Subject Re: [Math] Usage of "NullPointerException"
Date Fri, 06 Aug 2010 10:35:47 GMT
> What I propose (as an alternative the "simple" policy) above is to create
> a "NullArgumentException" that inherits from "MathIllegalArgumentException".
> This exception will be thrown whenever a null check fails (i.e. "null is an
> illegal argument"). To be consistent, we should never throw NPE from CM.

I'm converting all current uses of the "createIllegalArgument" over to
throwing "NullArgumentException".

While at that, I noticed there is another kind of inconsistency when dealing
with method arguments. In code like
        if (den == 0) {
            throw MathRuntimeException.createArithmeticException(
                  LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION, num, den);
One should throw a "ZeroException" (subclass of "IllegalArgumentException"),
instead of an "ArithmeticException", to folow the rationale that we settled
on (of throwing IAE instead of NPE for null arguments).

On another front, the separation between "general" and "specific" error
message patterns can save some more enums like in the above example (in the
"Fraction" class) where the modified code would look like:
        if (den == 0) {
            throw new ZeroException(LocalizedFormats.DENOMINATOR);
[Propagating the numerator is not relevant. Otherwise, where do you stop
in providing "context"? For every method you could consider that the whole
parameter list must be part of the detailed message...]

And, for the numerator check, we would have the equivalent:
        if (num == 0) {
            throw new ZeroException(LocalizedFormats.NUMERATOR);


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message