Apr 20
java.net: Exception-Handling Antipatterns:
E-mail Print Discuss Blog Exception-Handling Antipatterns Exception-Handling Antipatterns by Tim McCune 04/06/2006 * Contents * Basic Exception Concepts * Creating Your Own Exceptions * Exceptions and Transactions o EJB 2 o EJB 3 o Message-Driven Beans * Logging * Antipatterns o Log and Throw o Throwing Exception o Throwing the Kitchen Sink o Catching Exception o Destructive Wrapping o Log and Return Null o Catch and Ignore o Throw from Within Finally o Multi-Line Log Messages o Unsupported Operation Returning Null o Ignoring InterruptedException o Relying on getCause() * Conclusion * Resources Should you throw an exception, or return null? Should you use checked or unchecked exceptions? For many novice to mid-level developers, exception handling tends to be an afterthought. Their typical pattern is usually a simple try/catch/printStackTrace(). When they try to get more creative, they usually stumble into one or more common exception handling antipatterns. The antipattern concept became popular in the software development community with the release of AntiPatterns: Refactoring Software, Architectures, and Projects in Crisis in 1998. An antipattern draws on real-world experience to identify a commonly occurring programming mistake. It describes the general form of the bad pattern, identifies its negative consequences, prescribes a remedy, and helps define a common vocabulary by giving each pattern a name. In this article, we’ll discuss some fundamental concepts about the different types of Java exceptions and their intended uses. We’ll also cover basic logging concepts, especially as they relate to exception handling. Finally, instead of prescribing what to do, we’ll focus on what not to do, and take a look at a dozen common exception-handling antipatterns that you are almost certain to find somewhere in your code base.