Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Tools

Pragmatic Exceptions



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/


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.