Checked Exceptions Don’t (always) Suck

Steve Conover’s Weblog

February 17, 2003
Bruce Eckel Thinks Checked Exceptions are Bad

And I think he’s right

Checked exceptions aren’t necessarily bad, they’re just misused. A lot. Part of the problem is that all the documentation on the subject gives the impression that RuntimeExceptions are somehow more serious than checked exceptions, so most developers tend towards writing and throwing checked exceptions. Imagine the reverse: only a situation so serious that the calling code MUST decide what to do about it warrants a checked exception. Checked exceptions force the developer to explicitly deal with them, or declare that they don’t want to deal with them. Either way, a choice must be made. RuntimeExceptions on the other hand can be ignored, and collectively dealt with at some higher level in the application.

Doesn’t that make more sense?

2 thoughts on “Checked Exceptions Don’t (always) Suck

  1. Makes sense to me. As with the RMI example, there are times when you want to force to caller to acknowledge that something could go wrong – but it’s not something you could determine before the call (using a boolean method, for example). That’s a pretty good rule of thumb.

    It’s also one reason why I hedged and said “prefer unchecked exceptions” rather than “never use checked exceptions” đŸ˜‰

  2. I agree that the main problem is misuse of checked exceptions. In fact I probably would have mostly agreed with Bruce Eckel *before* I messed around with C#. It lacks checked exceptions, and in what little code I’ve written in C# I spent WAY too much time having to go back and add exception handlers around calls that I had no way of knowing might throw exceptions – the docs frequently didn’t mention them. Usually they were things that I could easily handle or prevent altogether, but it was largely trial and error. At least with Java’s checked exceptions the compiler gives help in that regard. So I’ve come to view checked exceptions almost as a “devil’s advocate” that makes me think seriously about what exceptions may occur, how I should handle them, and when should I create and throw a custom one.

    I would like to see a compiler option to generate warnings (not errors) for empty catch clauses. That would help with finding possible misuses. Or perhaps there is a style checker out there that can warn of such things?

Comments are closed.