Announcement Announcement Module
No announcement yet.
How to handle Exceptions thrown by an Advice? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to handle Exceptions thrown by an Advice?


    I'm trying to integrate some security functionality into an existing application that had no security concerns before, using a MethodInterceptor which should block the execution of specific methods while letting others proceed (depending on the user logged in). Actually everything works just fine. But my problem is the following:

    public Object invoke(MethodInvocation method) throws Throwable {
      if (user may not invoke method) {
        throw new Exception();
      if (user is allowed to invoke the method) {
        return method.proceed();
    Target Class:
    public class MyController {
      // this method will be intercepted:
      public List getSomeData() {
        return myDao.getMyData();
    The target method does not declare any exceptions so the caller does not catch any either. But I want the calling methods to catch the exceptions that can be thrown by the advice. The first thing to do (and the most inelegant one probably) would simply be modifying all the methods that interact with the advice. Thus, I would have thrown all the AOP principals out of the window. Isn't there any other possibility to make the advice interact with the caller?

  • #2
    Rather than throwing an instance of java.lang.Exception which is a checked exception, you could subclass RuntimeException (say UserLoginException) and throw an instance of that from the advice instead. That way you dont need to declare it or catch it and can let if fly back to the very top of the call stack and handle it on the presentation layer?

    Any help?


    • #3
      So it is the presentation layer where I can discern between allowed and prohibited method invocations.. Thanks for the help! I will see how I can implement it that way.