Code:Q

All | Technology | Entertainment | Java | Politics | XML | Travel | Ajax

20060201 Wednesday February 01, 2006

All exception constructors should take a String parameter

I've spent several days in code reviews. I kept seeing Exception classes with empty constructors, and that just drives me nuts. I also saw exception constructors taking only a Throwable. That drives me nuts almost as bad. After a few days I realized what the programming recommendation should be: They may take more than a String, of course, such as a Throwable, or an extra int, but there must be at least a String parameter.

Oh, and programmers that write
   throw new FooException("");
should be flogged.

(2006-02-01 17:52:36.0) Permalink Comments [3]

Trackback URL: http://jroller.com/trackback/matsh/Weblog/all_exception_constructors_should_take
Comments:

Hi,

Could you tell me the reasons of your opinion :

I kept seeing Exception classes with empty constructors, and that just drives me nuts. I also saw exception constructors taking only a Throwable. That drives me nuts almost as bad

Thank you,

Posted by hasan on February 02, 2006 at 10:16 AM CET #

<p>Requiring messages in constructors is more likely to discourage putting in the checks and those that remain are likely to have a meaningless comment. Let the professional programmer make the value decision, mindless rules will just get subverted.</p>

<p>If you get an exception the normal response is to check where it came from. If the message contains nothing that will help diagnose the bug, it'll just cause more work both at the time the code is maintained and when the exception is investigated. Assuming that the exceptions occurs in in-house code, there is probably little value in adding messages to the majority of throw sites.</p>

Posted by Tom Hawtin on February 02, 2006 at 08:37 PM CET
Website: http://jroller.com/page/tackline/ #

Tom,

a programmer that slapps on a meaningless string description to an exception doesn't sound very professional to me.

The rule isn't mindless! It is there to force me and everyone else to use text to describe why things fail.

Posted by Mats Henricson on February 02, 2006 at 10:27 PM CET
Website: http://www.henricson.se/mats #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: On

Please answer this simple math question

4 + 8 =