I found some syntax errors in your sample code. When I fix them, the following example works fine. BTW, I defined my own NoResultException because I don't have Java EE.
package javax.persistence; public class NoResultException extends RuntimeException { private static final long serialVersionUID = 1L; }
package com.example.stackoverflow; import javax.persistence.NoResultException; public class Application { public static void main(String[] args) { Application app = new Application(); System.out.println(app.valueReturningMethod(1, "two")); System.out.println(app.exceptionThrowingMethod(1, "two")); } public Object valueReturningMethod(int i, String string) { return "normal result"; } public Object exceptionThrowingMethod(int i, String string) { throw new NoResultException(); } }
package com.example.stackoverflow; import javax.persistence.NoResultException; aspect NullifyNoResultException { Object around() : execution(public Object com.example..*(..)) { try { return proceed(); } catch (NoResultException e) { return null; } } }
The output will be as expected:
normal result null
source share