Monday, June 27, 2011

Exceptionally Lazy Messages

In a perfecter world, wouldn't Java exceptions have been better defined with their messages as by-name arguments?:

class Throwable (message: => String, cause: Option[Throwable] = None)

Not that that's Java syntax, but you know what I mean.

Any number of times, I've gone to throw an exception with a nice detailed message, but confused myself by getting an exception during the construction of the message instead. The latter exception obscures the occurrence of the problem that would have thrown the former exception.

Maybe I'm the only one who writes message-building code complicated enough to have bugs of its own, but maybe not. I'm just sayin'.

2 comments:

  1. I have had that happen on many occasions. I try to throw a detailed exception, and it breaks. I fix something, but get the same error. After about a dozen iterations, I realize that getting the same message means the exception I constructed was failing. Grr! But this doesn't just happen with Java. I have had this happen many times in Python too.

    ReplyDelete
  2. I'm glad to know I'm not the only one.

    ReplyDelete