Java Tips: Generic Method

Generic is a pretty powerful addition to Java 5. But often people only use generic in the class and forget that generic can also be useful in method. This post will discuss a pretty neat use of generic to avoid casting and reduce duplication.

First, let start with the problem. Say that you have a lot of methods that catch exception and wrapped it with other exception.

public void methodName1() throws AnotherException1 {
   try {
      ...
   } catch (SpecificException1 e) {
       throw new AnotherException1(e);
   }
}

public void methodName2() throws AnotherException2 {
   try {
      ...
   } catch (SpecificException2 e) {
       throw new AnotherException2(e);
   }
}

Then, customer comes with another requirement that all exceptions should be logged before thrown. Here… the obvious solution is like this:

public void methodName1() throws AnotherException1 {
   try {
      ...
   } catch (SpecificException1 e) {
       AnotherException1 ex = new AnotherException1(e);
       log(ex);
       throw ex;
   }
}

public void methodName2() throws AnotherException2 {
   try {
      ...
   } catch (SpecificException2 e) {
       AnotherException2 ex = new AnotherException2(e);
       log(ex);
       throw ex;
   }
}

But that solution is not that elegant because we have to add two lines in every try catch. We can come up with a new method to simplify the change.

public Exception logAndReturnException(Exception e) {
   log(e);
   return e;
}

public void methodName1() throws AnotherException1 {
   try {
      ...
   } catch (SpecificException1 e) {
       throw (AnotherException1) logAndReturnException(new AnotherException1(e));
   }
}

public void methodName2() throws AnotherException2 {
   try {
      ...
   } catch (SpecificException2 e) {
       throw (AnotherException2) logAndReturnException(new AnotherException2(e));
   }
}

That’s remove duplication but not that pretty since we have to add casting in every place. Here, generic can be used to simplify and make our code prettier.

public <E extends Throwable> E logAndReturnException(E e) {
   log(e);
   return e;
}

public void methodName1() throws AnotherException1 {
   try {
      ...
   } catch (SpecificException1 e) {
       throw logAndReturnException(new AnotherException1(e));
   }
}

public void methodName2() throws AnotherException2 {
   try {
      ...
   } catch (SpecificException2 e) {
       throw logAndReturnException(new AnotherException2(e));
   }
}

Pretty neat, right? With the same technique we can simulate lambda calculus or function calling in Java. Try it!

Related posts:

  1. Java Tips: Simplify logging
  2. Java Tips: Using generic correctly
  3. Java Tips: Array argument
  4. Java Tips: Iterate and cast
  5. Java Tips: Memory Optimization for String

5 Responses to “Java Tips: Generic Method”


  • Why didn’t you create a logAndThrow method? And why didn’t you specify a supertype for E?

    public E logAndThrowException(E e) throws E {
    log.warn(e);
    throw (e);
    }

  • Oops, sorry for the markup issues, it should be:

    public <E extends Throwable> E logAndThrowException(E e) throws E {
    log.warn(e);
    throw (e);
    }

  • Good idea, but my point is that this is just an example on how to do it.

  • Generic is very nice ;) No more casting with ArrayList and Vector :D

  • @p3t0r: I just realized that your way won’t work because there is no way the main method know that this method will ALWAYS throw exception. The result is: it will ask for return statement, which I don’t want here.

Leave a Reply