Exceptions for action
Exception design for efficient error handling
By Jean-Pierre Norguet, JavaWorld.com, 11/15/07
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Exceptions in object-oriented applications tend to proliferate, overload the code, and improperly handle issues. In this article,
author Jean-Pierre Norguet explains how to design exceptions in order to implement a simple, readable, robust, active, debug-oriented,
and user-friendly error-handling system. He proposes the design of a sample exception set, including the source code of a
Java implementation. Finally, he explains how to integrate such a design into a Java enterprise application.
The best way to design exceptions in an object-oriented project is never as clear as we would like it to be. Exceptions tend
to proliferate in older and larger systems, eventually amounting to hundreds of lines of code. Checked exceptions are required
for some common programming scenarios but can create significant processing overhead. And silent catching has been shown to
be a source of trouble. Unfortunately, you cannot avoid these mishaps; you must, instead, learn to code your way through them.
In this article I show you that it is possible to satisfy the requirements of an error-handling system with a limited set
of exceptions. After establishing the foundation of a good error-handling system, I point out some of the common mistakes
in exception design that can undermine application performance. I then present a sample exception set that supports my basic
premise: that exceptions intended to help an external system (or user) deal with unexpected conditions should be designed
differently from those intended to help the programmer handle expected ones. I explain the semantics of my exception set and
also show how the various exceptions would flow through a typical Java enterprise application architecture. Finally, I show
you how to implement my exception set in Java.
Requirements of error handling
What is a good error-handling system? Besides aesthetic considerations, a good error-handling system is generally held to
meet the following requirements:
- It is triggered without failure in case of any kind of error in the system.
- It allows the application to automatically take the appropriate actions.
- The error message displayed to the user gives a clear description of what is wrong and what action must be taken to proceed.
- If assistance is required, the error message helps the user to interact with the help desk and gives the help-desk team the
necessary information to react easily and quickly.
- The logged information gives the development team the necessary information to identify the error, locate its source point
in the application code, and fix the issue.
- The error-handling code should not degrade code readability. While essential, error handling is only a safety net, and therefore
of less priority than the application's primary functions.
An error-handling system designed to meet these requirements is generally considered to be complete. The question that remains
for many Java developers is how to design an error-handling system that makes intelligent use of exceptions, and that is not
overloaded by them.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Download a Java implementation of Jean-Pierre's custom exception set.
- Kent Beck and Ralph Johnson introduced the presentation-business-persistence-integration layered application architecture
discussed in this article with the paper "Patterns generate architectures" (Eighth European Conference on Object-Oriented Programming, Lecture Notes in Computer Science, Volume 821; Springer Berlin / Heidelberg, 1994).
- "Beware the dangers of generic exceptions" (Paul Philion, JavaWorld, October 2003) explains how catching and throwing generic exceptions can quickly and quietly become
a source of trouble.
- "Designing with exceptions" (Bill Venners, JavaWorld, July 1998) is a foundation course on when and how to use exceptions.
- "Exceptional practices, Part 1" (Brian Goetz, JavaWorld, August 2001) offers guidelines for properly incorporating error handling into classes at design
time.
- See "Exceptions: Don't get thrown for a loss" (Tony Sintes, JavaWorld, February 2002) to learn more about the differences between checked and runtime exceptions.
- "Three Rules for Effective Exception Handling" (Jim Cushing, Java.net, April 2003) suggests that properly written exceptions should answer three fundamental questions:
What went wrong, where it did go wrong, and why it did go wrong.
- Lead C# Architect Anders Hejlsberg talks with Bill Venners and Bruce Eckel about versionability and scalability issues with
checked exceptions in "The Trouble with Checked Exceptions" (Artima Developer, August 2003).
- "Run from Runtime Exceptions (and Errors)" (Benjamin Booth, Dr. Dobb's Journal, March 2006) explains why you should run, not walk, from
IllegalArgumentExceptions and IllegalStateExceptions.
- See the JavaWorld Enterprise Edition Research Center for more articles like this one.
- You have questions about exception handling? Ask the JavaWorld developer community!