Re: Protection against NPEs/catching illegal arguments ASAP

From: Andrus (andru..bjectstyle.org)
Date: Thu Dec 05 2002 - 22:28:45 EST

  • Next message: Holger Hoffstätte: "Re: Revert function"

    I guess having an internal assertion framework is indeed better than just
    keeping endless if..else checks for errors (Please note that having said
    that I am still holding to my earlier opinion that using it for every set*
    method is evil).

    But we still need to solve a bigger problem - creating specific,
    informative exceptions (or assertions). They should provide as much context
    information as possible:

    "XYZ failed due to unmapped ObjAttribute (EntityABC.attr), parent DbEntity:
    (DbEntityABC)" or

    "Save failed for ArtistExhibit (State: NEW, ObjectId: {}), DataContext:
    123456: Missing relationship to Artist.", etc.

    And they should be easy to build - no string concatenation on the spot like
    we are doing now. Preferably message strings should be externalized using
    parameters and should be internationalizable.

    Oh well...

    Andrus

    At 10:00 AM 12/5/2002 -0600, Jonathan 'Wolf' Rentzsch wrote:
    >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 - 22:27:58 EST