Michael Scharhag

About Michael Scharhag

Michael Scharhag is a Java Developer, Blogger and technology enthusiast. Particularly interested in Java related technologies including Java EE, Spring, Groovy and Grails.

Java: Exception translation with AspectJ

Within this blog post I describe how you can use AspectJ to automatically translate one type of exception to another.

The problem

Sometimes we are in situations where we have to convert an exception (often thrown by a third-party library) to another type of exception. Assume you are using a persistence framework like hibernate and you do not want to leak hibernate specific exceptions out of a certain application layer. Maybe you are using more than one persistence technology and you want to wrap technology specific exceptions into a common base exception. In such situations, one can end with code like this:

public class MyRepository {
  public Object getSomeData() {     
    try {
      // assume hibernate is used to access some data
    } catch(HibernateException e) {
      // wrap hibernate specific exception into a general DataAccessException
      throw new DataAccessException(e);

Of course this becomes ugly if you have to do this every time you access a certain framework.

The AspectJ way

AspectJ is an aspect oriented programming (AOP) extension for Java. With AspectJ we can define Cross-cutting concerns that take care of the exception translation process for us.

To get started we first have to add the AspectJ dependency to our project:


Next we have to set up ajc, the compiler and bytecode weaver for AspectJ. This step depends on the developing environment you are using, so I will not go into details here. Eclipse users should have a look at the AspectJ Development Tools (AJDT) for Eclipse. IntelliJ IDEA users should make sure the AspectJ plugin is enabled. There is also an AspectJ Maven plugin available (check this pom.xml for an example configuration).

Now let’s define our aspect using AspectJ annotations:

public class ExceptionTranslationAspect {

  @Around("execution(* com.mscharhag.exceptiontranslation.repository..*(..))")
  public Object translateToDataAccessException(ProceedingJoinPoint pjp) throws Throwable {
    try {
      return pjp.proceed();
    } catch (HibernateException e) {
      throw new DataAccessException(e);

Using the @Aspect annotation we can declare a new aspect. Within this aspect we use the @Around annotation to define an advice that is always executed if the passed pointcut is matched. Here, the pointcut

execution(* com.mscharhag.exceptiontranslation.repository..*(..))

tells AspectJ to call translateToDataAccessException() every time a method of a class inside the com.mscharhag.exceptiontranslation.repository package is executed.

Within translateToDataAccessException() we can use the passed ProceedingJoinPoint object to proceed the method execution we intercepted. In this example we just add a try/ catch block around the method execution. Using the ProceedingJoinPoint instance we could also do more interesting things like analyzing the method signature using pjp.getSignature() or accessing method parameters with pjp.getArgs().

We can now remove the try/ catch block from the example repository implementation shown above and use a simple test to verify our aspect is working:

public class MyRepositoryTest {

  private MyRepository repository = new MyRepository();

  @Test(expected = DataAccessException.class)
  public void testExceptionTranslation() {


Using AspectJ we can easily automate the conversion of Java runtime exceptions. This simplifies our code by removing try/ catch blocks that would otherwise be required for exception translation.

  • You can find the full source of the example project on GitHub.


Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply

six + = 14

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: