commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <>
Subject [lang] NullArgumentException usage
Date Thu, 07 Jul 2011 16:56:44 GMT
Hi All:

I do like using NullArgumentException, but I find writing this over and over

if (arg == null) {
 thrown new NullArgumentException(argName);

How about this instead:

NullArgumentException.check(arg, argName);


something(NullArgumentException.check(arg, argName));

Depending on the style you like.

Where check is:

    public static <T> T check(T arg, String argName) {
        if (arg == null) {
            throw new NullArgumentException(argName);
        return arg;

Yes, you are pushing the argName on the stack (or passing it in a register)
and that is extra work, but you do not have to use the new method then ;)


Thank you,

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message