Even a chimp can write code

Thursday, April 22, 2004

Checked Exceptions

I don't know about you, but I often have a terrible time dealing with APIs that enforce checked exceptions. They are a good feature to have but their rampant misuse just defeats the whole beautiful idea. My understanding of exceptions is that they allow programmers to write less error checking code because this code could be delayed until a more appropriate point in the program. When done badly it is like trading today's problems for tomorrow's. For people that think all compile-time checks are better than any runtime checks [yes I happen to know such people], static type checking in their exception handling seems like manna from heaven.

Merely by virtue of its ability to channel errors up the call stack, one can justify the need for an exception handling mechanism. But programmers litter their code with try-catch clauses just because they have to and also to cover their asses -- not because they want to. Then there's those people who suffix every method signature with
throws Exception
These guys need to be rounded up and publicly flogged. This is just unforgivable!

Most code I have reviewed doesn't have any impressive exception handling. Rather the focus is on protecting against that exception (like de-allocating memory, closing open connections etc. as the case may be). If your chief way out in case of an exception condition is to log a message or popup a dialog for the user, then it makes sense to have a centralized exception handler that acts as a bucket for all such critters.

Bill Venners and Bruce Eckel [two smart dudes] interviewed Anders Hejlsberg, the C# big honcho, about checked exceptions and the versionability and scalability issues that come with it. It is an interesting read. But here's a hilarious -- and smart -- critique of that interview by James Ross that made me fall out of my chair laughing. It was only my earphone cord, plugged to the laptop, that prevented my head hitting the floor.

This issue's being talked about a good deal of late. There's some great ideas thrown around. More soon.

Email this | Bookmark this