The Source for Java Technology Collaboration
User: Password:



Start New Message Delete Post a Reply

Article: 
 Exception-Handling Antipatterns
Subject:  Most of the examples in this article expose the design flaws of Java.
Date:  2008-05-07 11:52:29
From:  cpk1971


Some general comments:

1. Many of these antipatterns (Throwing Exception, Kitchen Sink, Catching Exception, Log and Return Null) are caused by Java's insistence on checked exceptions. I wrote in .NET for several years and never saw code like this:

try {
// stuff
} catch (Exception e) {}

NEVER. If you didn't handcuff people with checked exceptions, people wouldn't react in this way.

Checked exceptions are simply evil. James Gosling is simply wrong on this point (it's about the only thing he's ever been wrong about, but boy is he wrong). The benefit it gives you is massively outweighed by the problems it causes, not the least of which are the behaviors it encourages in mediocre programmers.

2. Log and throw is OK if worker classes have their own logs. This way you can see the exception in context with the other processing steps. You don't want logs of intermediate processing steps combined in high-level logs anyway, so really this is just an argument against putting everything in the same log.

3. Throwing From Finally is a flaw in the try/catch/finally, and you are basically just screwed. This can cause serious, serious problems and simply logging the error is insufficient. Java gives us no other alternatives, however.

Java is a decent platform, but when it comes to exceptions it is made of total fail.

 Feed java.net RSS Feeds