My 2 Cents on Checked Exceptions
August 8, 2006 – 21:37 | javaThis might sound philosophical and irrelevant, but hear me out - the biggest problem I have with checked exceptions is that they are way too prescriptive. When we say “checked exceptions force themselves to be handled,” that’s an understatement of the prescriptiveness. What’s worse is actually that checked exceptions impose this forcement on the immediate caller.
So what are the practical issues caused by this overly strong prescriptiveness? After all, we are subject to all kinds of prescriptive annoyances in our daily programming life (and perhaps non-programming life also for that matter). If it’s just some philosophical thing, why bother, right?
Well, it’s inconsistent in programming practices, to begin with. Let’s say we have a method int foo(). In one particular usage scenario we don’t really care much about the return value of foo(), so we would simply put foo();. But you can’t completely ignore it if foo() throws a checked exception. At least you have to try/catch or rethrow it, point here being the immediate caller must be aware of the presence of that exception. What if the next version of javac makes foo(); illegal, and enforces that any method with non-void return type must have its return value assigned to a variable? Most of us would no doubt think that’s just ridiculous, and yet we somehow consider it’s reasonable to mandate the handling of checked exceptions, which in some broad sense are just return values.
And speaking of return values, that leads to another practical issue. We all know about the notorious flame bait called “should XXX be in the return value or an exception?” IMHO, checked exceptions, if not the root cause of that big confusion, have definitely been fueling it by blurring the distinction between return values and exceptions. People have much less problem determining whether XXX should be a return value or an unchecked exception. Why? Because the nature of unchecked exceptions carries a clear message that they are unexpected, and completely out of band - which is what all the exceptions should have been, per the very definition of word “exception” itself. So it’s that much easier to look at getAccount() and decides that it should return null instead of throwing AccountNotFoundRuntimeException, because “account not found” should be expected. On the other hand, since AccountNotFoundException must be handled by the caller - and hence must be fully expected by the caller, we now have two options to return a piece of information back to the caller, and both are equally effective in keeping the caller’s “level of expectation”. When there are more than one option and none is obviously better than others, guess what, there’s gonna be some debate.

3 Responses to “My 2 Cents on Checked Exceptions”
The whole point of checked exceptions is that they must be handled, requiring at least _some_ thought. Consider what it used to be like in C - functions used return values to indicate errors, and they were never looked at.
When desiging your own APIs, good use of the Null Object pattern can work here. For example, if you don’t want to have an “AccountNotFoundException”, you could provide an “accountExists” method (returning true or false), plus have the getAccount method return a NullObject Account that barfs if it is used. Callers who respect the API will never encounter the problem, and callers who don’t respect the API still get exceptions.
By Robert on Aug 9, 2006
I understand that, and am precisely against the “must” and “requiring” part - attention needs to be paid to _who_ is requiring _whom_ here. A callee should only put requirement on the _input_, not the _output_. How would you like it if your favorite restaurant made you sign a formal contract to guarantee you’ll consume the food you are taking home?
By Jing Xue on Aug 9, 2006