Announcement Announcement Module
Collapse
No announcement yet.
Throwing exception in @Before, catching in @AfterThrowing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Throwing exception in @Before, catching in @AfterThrowing

    Hi,

    i created a @Before Advice that throws an exception and tried to catch it in another @AfterThrowing, but it does not work.

    if the exception is not thrown in the advice, but directly in the method, it works.

    if it is thrown in the advice, the @AfterThrowing is not executed.

    Why does it behave like that?

    Thanks!

  • #2
    The ordering of the advice... I suspect the afterthrowing is applied AFTER the Before and thus it never sees the exception, change the order.

    Comment


    • #3
      hmm, that does not change anything.
      The point is, that if the Before Advice does not throw an exception, the After Advices are executed at the right time.

      Comment


      • #4
        Because it is applied after the before advice... So when you are in your method the advice is applied.

        Code:
        advice1 -> advice2 -> method
        Advice 1 is your before
        Advice 2 is your after throwing

        So if you before is throwing an exception that is not going to be seen by the after throwing advice.

        Comment


        • #5
          I changed it

          AfterThrowing Advice has Order: Ordered.HIGHEST_PRECEDENCE + 4
          BeforeAdvice has Order: Ordered.HIGHEST_PRECEDENCE + 5

          So the AfterThrowing should be executed first. But it does not work...

          thx

          Comment


          • #6
            Post your code and configuration...

            Also Ordered.HIGHEST_PRECEDENCE is actually Integer.MAX_VALUE so adding something makes it run out of the integer range! Which actually leads to your Before advice being executed before the after advice!

            AfterThrowing: -2147483645
            Before: -2147483644
            Last edited by Marten Deinum; Mar 21st, 2012, 05:34 AM.

            Comment


            • #7
              HIGHEST_PRECEDENCE is Integer min value ( -2147483648 )

              code

              Code:
              @Aspect
              @Order(Ordered.HIGHEST_PRECEDENCE + 5)
              public class MyBeforeAdvice
              {
                 @Before("execution(* *(..))")
                 public void throwEx(JoinPoint jp)
                 {
                     throw  new IllegalArgumentException("TEST");
                 }
              }
              Code:
              @Aspect
              @Order(Ordered.HIGHEST_PRECEDENCE + 4)
              public class MyAfterThrowingAdvice
              {
                 @AfterThrowing(pointcut = "execution(* *(..)) ",throwing = "e")
                 public void convertExceptions(JoinPoint joinPoint, Throwable e)
                 {
                      System.out.println("catching " + e.toString());
                 }
              Code:
              <aop:aspectj-autoproxy />
              
              <bean id="beforeBean" class="at.test.MyBeforeAdvice" />
              <bean id="afterThrowingBean" class="at.test.MyAfterThrowingAdvice" />

              Comment


              • #8
                I should read and rethink before I write ..

                I wouldn't put algebra into a annotation but simply specify which to go first but that is just me...

                Looking at the sources for spring and the ordering the after is always order after other advices next to that the ordering used doesn't do anything with the @Order annotation instead of the annotation you could try the Ordered interface or the @DeclaraPrecedence annotation (which would make it more an Aspect !)

                Comment


                • #9
                  i changed the @Before Aspect to an @Around - and it works ;-)

                  Comment

                  Working...
                  X