The Artima Developer Community
Sponsored Link

Java Community News
Exception-Handling Anti-Patterns

5 replies on 1 page. Most recent reply: Apr 17, 2006 9:56 PM by Minkoo Seo

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Exception-Handling Anti-Patterns Posted: Apr 7, 2006 7:54 AM
Reply to this message Reply
Summary
While Java's exception handling mechanisms aid the creation of more robust code, few developers take full advantage of exceptions. A recent article discusses common exception-handling anti-patterns, and suggests some remedies.
Advertisement

A few days ago, we pointed here on Artima Developer to an interview with Ron Goldman, where he talks about the importance of writing code that ensures the robustness of an application:

In computing, perhaps 5% of our code deals with exception handling and error correction, which seems like a lot, while 95% tries to get the basic job done. Biology appears to reverse this, with 5% doing the basic metabolism and 95% functioning to make sure that the 5% can do its job.

Java provides a sophisticated exception-handling mechanism, but few developers take full advantage of that facility. A recent article by Tim McCune points out a few common exception-handling anti-patterns, and suggests ways to better handle exceptions. Among the anti-patterns he shows are:

  • Log and throw
  • Throwing Exception
  • Throwing the kitchen sink
  • Catching Exception
  • Destructive wrapping
  • Log and return null
  • Catch and ignore
  • Throw from within finally
  • Multi-line log messages
  • Unsupported operation returning null
  • Ignoring InterruptedException
  • Relying on getCause()
What other exception-handling anti-patterns have you observed in Java code?


maninder batth

Posts: 4
Nickname: anywhere
Registered: Mar, 2006

Re: Exception-Handling Anti-Patterns Posted: Apr 7, 2006 2:23 PM
Reply to this message Reply
First of all i think the article was extremly shallow. Not much detail and no real out of the box thinking. For example
here is my pet
"This is one of the most annoying error-handling antipatterns. Either log the exception, or throw it, but never do both. Logging and throwing results in multiple log messages for a single problem in the code, and makes life hell for the support engineer who is trying to dig through the logs."

TRUE, if you are really silly to log it again. For example, if this is an online application, Maybe you want to log the exception and wrap it with some generic exception to be sent to the ui layer and the generic message handling code inspects the exception and generates a message for the end user. Maybe you want to log it, wrap it and send the message to different component who is interested in why the request failed. Maybe Maybe ...... there are many scenarios, but the author makes it sounds like that the habit of log n rethrow is a sin

In my opinion rambling never helps.

Yes exception handling is not done nicely, but then propose a framework, propose pattens which developers can leverage.
That to me is the key to this problem.

Jim Shi

Posts: 7
Nickname: jim2000
Registered: May, 2005

Re: Exception-Handling Anti-Patterns Posted: Apr 8, 2006 9:47 PM
Reply to this message Reply
The real problem is that checked exceptions are not that great as originally envisioned. SUN just has to
admit the mistake and do away with checked exceptions althoether.

V.H.Indukumar

Posts: 28
Nickname: vhi
Registered: Apr, 2005

Re: Exception-Handling Anti-Patterns Posted: Apr 10, 2006 2:28 AM
Reply to this message Reply
Checked exceptions are good in a limited field of influence. The problem was that Sun introduced it without any best practices and good advices. Looking into their code, it seems that they themselves had no idea how to leverage it.

IMHO, checked exceptions should be used sparingly and they should be used only if there is a definite possibility that the caller could handle and _recover_ from it. Instead, most of the Java APIs just use checked exceptions as some kind of a rule.

Another thing that the author has failed to mention is that exceptions are first class objects. Most of the code I have seen store only the message in Exceptions. What I do not understand is why does not 'FileNotFoundException' have another member 'fileName' in the class (i.e. FileNotFoundException.getFileName())? Exceptions should have richer information and should not be limited to just a message. This way the caller will have more structured information at its disposal.

Andreas Mross

Posts: 12
Nickname: amross
Registered: Apr, 2004

Re: Exception-Handling Anti-Patterns Posted: Apr 10, 2006 6:27 PM
Reply to this message Reply
I've seen some really useful applications of checked exceptions. When done right, they result in very robust software with little extra effort from the programmer. Unfortunately, I've seen many more misuses of them.

I think ultimately, checked exceptions are like operator overloading; a useful tool in theory, but in practise an easy way to suffer at the hands of other programmers. I've used libraries where every single method call throws a generic checked exception, or where you could know just by looking at the code that an exception could never be thrown, but were forced to catch the exception anyway.

For that reason, I think they go against the Java philosophy, of making it easy and safe to use other programmer's code. Sadly, Java would be better of without them.

Minkoo Seo

Posts: 3
Nickname: minkoo
Registered: Nov, 2005

Re: Exception-Handling Anti-Patterns Posted: Apr 17, 2006 9:56 PM
Reply to this message Reply
[quote]
SUN just has to
admit the mistake and do away with checked exceptions althoether.
[/quote]

Can you show me the reference, so I can confirm SUN's comment?

Thanks.

Flat View: This topic has 5 replies on 1 page
Topic: Exception-Handling Anti-Patterns Previous Topic   Next Topic Topic: Testing Legacy Code

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use