Re: Protection against NPEs/catching illegal arguments ASAP

From: Jonathan 'Wolf' Rentzsch (webobject..edshed.net)
Date: Thu Dec 05 2002 - 11:00:00 EST

  • Next message: Craig Miskell: "Revert function"

    Holger Hoffstätte, holge..izards.de, wrote:
    >Catching illegal arguments and especially potential NPEs as early as
    >possible is IMHO a Good Thing - don't let errors dribble down the layers
    >until the bottom drops out somehwere else. Easiest & IMHO most obvious
    >place to do at least some form of checking would be the setters, and have
    >them throw an IllegalArgumentException("foo must not be null"), or
    >whatever is appropriate. Classes without public accessors could have them
    >protected/private I usually start with private and 'open up' as required).

    Perhaps my JAssert code may be helpful here?

    <http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/redshed/JAssert/>
    <http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/redshed/JAssert/JAssert.jav
    a?rev=1.1.1.1&content-type=text/vnd.viewcvs-markup>

    (I need to javadoc that class.)

    I'll be adding some form of explicit argument checking in the future.
    Something like:

    void foo( Object bar ) {
      JAssert.arg.notNull( bar );
    }

    What's nice is that the assertion failure is swappable at runtime, so you
    could decide whether to throw an IllegalArgumentException,
    NullPointerException, Log4J.error(), or whatever.

    .......................................................
    Jonathan 'Wolf' Rentzsch jon at redshed dot net
    Red Shed Software http://redshed.net (847) 584-7465
    PGP: B2AF 1A09 F881 EBDE C9D6 C4D2 C04F A3C0 3EC5 D5F2

                 "There are 10 types of people:
        those who count in binary, and those who don't"



    This archive was generated by hypermail 2.0.0 : Thu Dec 05 2002 - 11:00:12 EST