Pragmatic Exceptions

Tip #8: Run from Runtime Exceptions (and Errors)


April 01, 2006
URL:http://www.drdobbs.com/tools/pragmatic-exceptions/184406489


Here we go. Avoid unchecked (runtime) exceptions and errors unless absolutely necessary. For me, following my own advice means giving up my old bar buddies IllegalArgumentException and IllegalStateException. Sound crazy? Joshua Bloch, author of Effective Java would say so.

This is a controversial tip, but I stand by it. Let me begin by explaining both arg types:

Individualist-clean-freaks say: Throwing too many checked exceptions clutters up the API and also makes it harder to use. Plus, the appropriate runtime exception already exists; why not use it? API users must handle try/catches constantly. What a pain! Plus, it slows down your app. Checked exceptions suck.

Socialist-do-gooders say: It's important to declare what can go wrong explicity and force people to consider how to handle specific cases. This encourages the construction of a more bullet-proof application. Throwing an unchecked exception doesn't give people a chance to consider the appropriate response to an error until it happens—when the thing is actually running (or being tested). Then, it may be too late.

To the dismay of libertarians everywhere, put me in the socialist-do-it-righter camp on this one. But there's more here than meets the eye.

Think about it. "Forcing" someone to deal with your checked exception actually is giving them a choice (libertarians breath easier now), while throwing unchecked exceptions destroys exception handling free-will by ensuring there's no alternative.

Running from runtime exceptions primarily applies to static typed languages like Java. Lisp and Ruby folks would laugh at such grade-school advice, but as a general rule, it's safest to use checked exceptions. In Java, this means always throwing subclasses of java.lang.Exception.

In the rare event that an exception thrower is sure that the application must die, only then should they murder it with a runtime exception. Bill Venners summarizes this pretty well in his article "Designing With Exceptions" (JavaWorld, July 1998; http://www.javaworld.com/javaworld/jw-07-1998/jw-07-techniques.html):

In general, you should throw an exception and and never throw errors. Error, a subclass of Throwable, is intended for drastic problems, such as OutOfMemoryError, which would be reported by the JVM itself. On occasion, an error, such as java.awt.AWTError, could be thrown by the Java API. In your code, however, you should restrict yourself to throwing exceptions (subclasses of class Exception). Leave the errors to the big guys.

Think twice before catching subclasses of Error or RuntimeException. If the pitcher was careful enough to throw a Runtime or Error exception type, then what could possibly make higher-level catchers confident enough to process it and continue on as if all is okay (such as in an OutofMemoryError)?

—Benjamin Booth

http://www.benjaminbooth.com/

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.