0

I have a webapp using Spring + Hibernate through JPA, Spring is effectively translating all persitence exceptions into variations of DataAccessException.

I am now trying to catch these DataAccessExceptions in an AOP pointcut like this:

@Aspect
public class AspectException {


    @AfterThrowing(pointcut = "within(com.mypackage.dao..*)", throwing = "error")
    public void catchException(Throwable error) {
      MyOwnException e = new MyOwnException("ERROR!", error);
       throw e;
    }
}

Now this aspect works if in my DAO I throw a custom exception, but won't work with Spring's DataAccessExceptions!

Any idea why this is happenning?

M Rajoy
  • 4,028
  • 14
  • 54
  • 111
  • Actually I find it hard to believe that this should work for one type of exception and not for another, unless your advice parameter is not typed `Throwable` as in your example but of a specific sub-type not matching the `DataAccessException`. Even the [Spring manual](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-advice-after-throwing) mentions `DataAccessException` as a working example for `@AfterThrowing`. – kriegaex Apr 06 '15 at 00:01

1 Answers1

0

Have you throught about exception translation?

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html#jdbc-SQLExceptionTranslator

Brian Kates
  • 480
  • 4
  • 14
  • I want to translate all persistence exceptions into one of my own – M Rajoy Mar 30 '15 at 21:18
  • Can I ask why you want to translate all exceptions? What about something like this: http://stackoverflow.com/questions/14729227/exception-handling-strategy-with-spring-jpa-jsf – Brian Kates Mar 30 '15 at 21:50
  • Also, see this thread: http://stackoverflow.com/questions/24736954/spring-data-jpa-hibernate-catch-block-inside-a-transactional-method-is-never – Brian Kates Mar 30 '15 at 21:52
  • I want to translate them into an annotated @WebFault exception with Faultbean for a WebService specification. I'll take a look at those links, thanks! – M Rajoy Mar 30 '15 at 22:04