commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilles Sadowski <gil...@harfang.homelinux.org>
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
---CUT---
        if (den == 0) {
            throw MathRuntimeException.createArithmeticException(
                  LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION, num, den);
        }
---CUT---
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:
---CUT---
        if (den == 0) {
            throw new ZeroException(LocalizedFormats.DENOMINATOR);
        }
---CUT---
[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:
---CUT---
        if (num == 0) {
            throw new ZeroException(LocalizedFormats.NUMERATOR);
        }
---CUT---


Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message