Pragmatic Exceptions

Dr. Dobb's Journal November, 2005


Tip #3: The Bird's-Eye View Knows What To Do

The best view of your program is the bird's-eye version. The top of the execution stack is the best context for deciding what to do with the exception-hot potato.

It's also no accident that the higher in the stack you go, the closer you get to the application user's perspective. Because this is the perspective you (as the exception catcher) should be most concerned about, it follows that the top of the execution stack is where the best exception handling can occur.

If "you're it" as the exception catcher for the job, your options include:

If the current exception doesn't seem like an application killer but you're not sure, play it safe and follow Tip #1—If In Doubt, Throw It Out. This assumes you're not the highest-level catcher. You might try Refactoring for exceptional clarity to give yourself more certainty.

Exiting the application is an option, but this is reserved for the most fatal of errors. Assuming it's not one of these and knowing exceptions may be poisonous, youÕre left handling the exception somehow. The rubber finally meets the road. Here, you have three kinds of choices:

In picking from the first two options, you can Make Only the Actionable Obvious (Tip #4; available in next month's issue) to give us a simple heuristic for making the right decision.

—Benjamin Booth
benjamin.booth@gmail.com