Results 1 to 9 of 9

Thread: Throwing exception in @Before, catching in @AfterThrowing

  1. #1
    Join Date
    Mar 2012
    Posts
    5

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    The ordering of the advice... I suspect the afterthrowing is applied AFTER the Before and thus it never sees the exception, change the order.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Mar 2012
    Posts
    5

    Default

    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.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Mar 2012
    Posts
    5

    Default

    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

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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 at 05:34 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Mar 2012
    Posts
    5

    Default

    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" />

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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 !)
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Mar 2012
    Posts
    5

    Default

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •