Results 1 to 9 of 9

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

Hybrid View

  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,625

    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,625

    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,625

    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

Posting Permissions

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