Friday, March 16, 2012

Writing exception messages.

I recently changed an exception message from this:
throw new IllegalStateException("It appears the init() function has not been called.");
To this:
throw new IllegalStateException("private member object 'thingamagjig' is null. Has the init(T thingamajig) function been called?");

This is because I realized that there's a variety of reasons that the 'thingamajig' might be null, aside from the init() function not having been called to initialize thingamajig.  It was more important to communicate the actual diagnosed problem and to clearly communicate the possible cause of it, than to jump right to the conclusion that init() hasn't been called.   The question is a good rhetorical cue that "maybe you should look here first" without saying "this is the cause, dummy" and perhaps being the dummy instead.

No comments:

Post a Comment