Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] "About execution flow of advices when occur an exception"

Dear Henrique,

> -However, consider in the first advice if we change the boolean value of x
> to True;
>
>
> If we execute the code above with this modification, we will have the
> result:
>
> Code to execute before all non-static method of Class C
>
> Code to execute after all non-static method of Class C
>
> Exception in thread "main" java.lang.Error: error<before>!
> ...
>  The question is: Why after the first advice threw an error
> (java.lang.error) and the last advice executed???
>
> -It was supposed to threw an error and stops..

No, it wasn't. The semantics of AspectJ for after advice is to execute
it no matter what, even if an exception is thrown before it executes,
What happens in this case is that the advice is executed and, only
then, the exception (or Error, in your case) is propagated upwards
(and, hence, the error message is printed AFTER the advice's). If you
wanted the advice to run only if no exceptions were thrown, you'd have
to change the last advice to

after(C current) returning (Object o): ...

In this manner, the advice will only be executed if no exceptions are thrown.

>  Code to execute before all non-static method of Class C
>
> Code to execute after all non-static method of Class C
>
> Exception in thread "main" java.lang.Error: error<after>!
>
>  Now we see that the first advice caused the error, and besides executed the
> first, it executed the last... the wird thing that we can notice is that the
> error message printed has the sentence of the last advice insted the first
> (error<before>). So, I dont undestand this execution flow...

This is also expected. If an after advice throws an exception, it
ignores any exceptions that were thrown prior to its execution,
similarly to a finally {} block in Java. That's why you got an error
message regarding the last advice, instead of the first one.

Hope this helps.

Kind regards,
Fernando


Back to the top