After-throwing advices are written using the annotated parameter signature. The only difference is that these advices feature an additional annotated parameter: @Thrown. This mandatory parameter will hold the thrown exception. So, an after-throwing advice should be of the form:
public void <any-method-name>(@<Annotation> <any type> arg0, @<Annotation> <any type> arg1, ... @Thrown Throwable argi, ... @<Annotation> <any type> argN) throws <Exception1>,<Exception2>,...,<ExceptionN>
Where 0 <= i <= N.
To bind an after throwing advice to a pointcut, simply insert the <throwing> tag in a binding xml declaration, as in the following example:
<bind pointcut="execution(* POJO->*(..))"> <throwing name="afterThrowing" aspect="Aspect"/> </bind>
Differently from other advice types, after-throwing advices are not called everytime an intercepted joinpoint gets executed. On the contrary, after-throwing advices are invoked only when the joinpoint throws an exception. So, in the binding example above, this means that afterThrowing will be invoked only after a POJO method throws an exception, instead of being invoked after all POJO methods' executions.
You can find examples of after-throwing advices in the Aspect.java file.
The following advice signature:
public void afterThrowing(@Thrown Throwable thrown)
Is the simplest you can write, since the @Thrown Throwable parameter is mandatory.
But you can also include any other annotated parameter (except @Return, which is exclusive of after advices).
The following advice signature is valid to intercept a method execution throwing an exception:
public void afterThrowingJoinPoint(@JoinPoint MethodExecution methodExecution, @Thrown Throwable thrown)
Notice that you can declare the annotated parameters in any order you wish.
If the method being intercepted receives an String argument, these after throwing signatures are also valid:
public void afterThrowingArg(@Thrown Throwable thrown, @Arg String argument) public void afterThrowingJoinPointArg(@Thrown Throwable thrown, @JoinPoint MethodExecution methodExecution, @Arg String argument)
THIS EXAMPLE REQUIRES JDK 5!! For other options, please look at the annotated examples guide To compile and run:
$ run.aopc.50
It will javac the files and then run the AOPC precompiler to manipulate the bytecode, then finally run the example. The output should be similar to this:
_run.aopc.50: [java] Calling POJO->throwExceptionMethod() [java] ==================================== [java] RUNNING POJO->throwsExceptionMethod("argument") [java] >>> afterThrowing: java.lang.Exception: POJO Exception [java] >>> afterThrowingJoinPoint: java.lang.Exception: POJO Exception [java] >>> afterThrowingArg: java.lang.Exception: POJO Exception [java] >>> afterThrowingJoinPointArg: java.lang.Exception: POJO Exception [java] Caching Exception java.lang.Exception: POJO Exception [java] Calling POJO->throwNothingMethod() [java] ================================== [java] RUNNING POJO->throwNothingMethod() [java] No Exception this time