| [aspectj-users] "About execution flow of advices when occur an exception" |
public class C {
public void m(int a, int b){
System. out.println("Method m()");
}
}
public aspect AspectoC {
before(C current) :
execution(!static * C.*(..)) &&
within(C) &&
this(current){
boolean x = false ;
System.out.println( "Code to execute before all non-static method of Class C");
if (x){
throw new Error("error<before>!" );
}
}
before(C current) :
execution(void C.m(int ,int)) &&
within(C) &&
this(current){
System.out.println( "Code to execute before execution of method m(int, int)");
}
void around (C current) :
execution(void C.m(int ,int)) &&
within(C) &&
this(current){
proceed(current); // executed after proceed() call
System.out.println( "Code to execute after execution of method m(int, int)");
}
after(C current) :
execution(!static * C.*(..)) &&
within(C) &&
this(current){
boolean x = false ;
System.out.println( "Code to execute after all non-static method of Class C");
if (x)
throw new Error("error<after>!" );
}
}
If we execute this code above, we will have the result:
Code to execute before all non-static method of Class C
Code to execute before execution of method m(int, int)
Method m()
Code to execute after execution of method m(int, int)
Code to execute after all non-static method of Class C
About this result it is ok!!!
-However, consider in the first advice if we change the boolean value of x to True;
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>!
at teste.AspectoC.ajc$before$teste_AspectoC$1$397accb1(AspectoC.aj:13 )
at teste.C.m(C.java:6)
at teste.CodigoCliente.main(CodigoCliente.java:14 )
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..
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>!
at teste.AspectoC.ajc$after$teste_AspectoC$4$397accb1(AspectoC.aj:42 )
at teste.C.m(C.java:6)
at teste.CodigoCliente.main(CodigoCliente.java:14 )
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...