FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Java
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
April 01, 2006
Pragmatic Exceptions

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


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 ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Looking for a new job? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:



    MICROSITES
    FEATURED TOPIC

    ADDITIONAL TOPICS

    INFO-LINK



     




    Techweb
    Informationweek Business Technology Network
    InformationweekInformationweek 500Informationweek 500 ConferenceInformationweek AnalyticsInformationweek Events
    Informationweek MagazineGlobal CIOIWK Government ITbMightyByte and SwitchDark Reading
    Digital LibraryIntelligent EnterpriseInternet EvolutionNetwork ComputingPlug Into The CloudDr. DobbsContentinople
    space
    TechWeb Events Network
    InteropVoiceConWeb 2.0 ExpoWeb 2.0 SummitEnterprise 2.0Mobile Business ExpoNoJitter
    Black HatGTECEnergy CampCloud ConnectGov 2.0 ExpoGov 2.0 Summit
    space
    Light Reading Communications Network
    Light ReadingLight Reading AsiaUnstrungCable Digital NewsInternet EvolutionPyramid Research
    Heavy ReadingLight Reading LiveLight Reading InsiderEthrnet ExpoTelco TVTower Technology Summit
    space
    Financial Technology Network
    Advanced TradingBank Systems and TechnologyInsurance and TechnologyWall Street and TechnologyAccelerating WallstreetBST SummitBuyside Trading SummitIT Summit
    space
    Microsoft Technology Network
    MSDNTechNetTotal IT ProTotal Dev ProNET Total Dev Pro CommunitySQL Total Dev Pro Community
    space