Exceptions for action

Exception design for efficient error handling

1 2 3 4 Page 4
Page 4 of 4

Extending from java.lang.Exception

The exception design I propose can be easily implemented in any object-oriented language, including Java. In fact, a similar exception tree is already provided in the standard Java library. In this library, exceptions are modeled as java.lang.Throwable, checked exceptions are modeled as java.lang.Exception, and runtime exceptions are modeled as java.lang.RuntimeException.

Under java.lang.Exception, a number of business exceptions with a broad panel of semantics are provided. Runtime application exceptions like ParameterException, TechnicalException, and CriticalTechnicalException (shown in Figure 1), are respectively and roughly modeled as IllegalArgumentException, MissingResourceException, and IllegalStateException.

In your application, reusing standard Java exceptions is an interesting idea, but it could introduce some confusion with the exceptions thrown by the standard Java classes. You could avoid this confusion by defining your own tree, extending java.lang.Exception. Defining your tree also allows you to implement exception nesting and issue IDs.

Listing 1 shows my sample exception set implemented in Java code. Note that it includes exception nesting and id attributes.

Listing 1. Java code implementing exception nesting and issue identification

public class NestedException extends RuntimeException {
   protected Exception nestedException;
   protected int issueId;
   public NestedException(String msg, Exception e, int id) {
      this.nestedException = e;
      this.issueId = id;
   public Exception getNestedException() {
      return this.nestedException;
   public int getIssue() {
      return this.issueId;
public interface Issue {
   public final static int UNDEFINED = 0;
   public final static int EXTERNAL_SERVICE_1_DOWN = 1;
   public final static int EXTERNAL_SERVICE_2_DOWN = 2;
   public final static int SQL_STATEMENT_ERROR = 3;
   // ...

In conclusion

Designing a limited exception tree that fulfills the requirements of a good error-handling system is very simple. The secret of this simplicity resides in focusing your design effort on defining the kinds of actions the system should take, rather than on the kinds of issues that may arise. In this design, information about the issue is encapsulated inside the exception. Dispatching the semantics between actions and issues this way allows you to narrow the exception tree to a limited set of exceptions -- perhaps a half dozen. Not only does this design limit exception proliferation but it keeps your code readable, which subsequently allows you to focus on coding the application's business logic with optimal clarity.

Jean-Pierre Norguet holds a Ph.D. in computer science and network engineering from the University of Brussels. After three years of full-time Java development with IBM on mission-critical e-business applications, as team leader and coach, his areas of expertise grew to include the entire application development lifecycle. His research currently focuses on Java technology, ontology integration, and Web usage mining. In addition to several Java articles online, Dr. Norguet has published JEE books with Prentice Hall and IBM Press, as well as several articles in international research conferences of ACM, IEEE, and Springer-Verlag. His outside interests include artistic drawing, French theater acting, and alternative healthcare.

Learn more about this topic

This story, "Exceptions for action" was originally published by JavaWorld.

Copyright © 2007 IDG Communications, Inc.

1 2 3 4 Page 4
Page 4 of 4