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-Type> argi, ... @<Annotation> <any type> argN) throws <Exception1>,<Exception2>,...,<ExceptionN>
Where 0 <= i <= N and Throwable-Type can be one of the following:
java.lang.Throwable
any runtime exception
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 advice in the Aspect.java file.
The following advice signature:
public void afterThrowing(@Thrown Throwable thrown)
Is the simplest you can write, since the @Thrown-annotated 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 a
POJOException
exception:
public void afterThrowingJoinPoint(@JoinPoint MethodExecution methodExecution, @Thrown POJOException thrown)
Notice that you can declare the annotated parameters in any order you wish.
If the method being intercepted receives an String argument, and you want to intercept that method throwing RuntimeExceptions, these after-throwing signatures are also valid:
public void afterThrowingArg(@Thrown RuntimeException thrown, @Arg String argument) public void afterThrowingJoinPointArg(@Thrown NullPointerException thrown, @JoinPoint MethodExecution methodExecution, @Arg String argument)
Note that the first one receives a RuntimeException as @Thrown-annotated parameter. Thus, this advice is going to be invoked only when the intercepted method throws a RuntimeException. The second advice is more specific regarding this parameter, and will be invoked only when the intercepted method throws a NullPointerException.
To compile and run (for further detail, refer to our Compiling and Running Examples Guide):
$ ant run.aopc
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: [java] [java] Calling POJO->throwPOJOExceptionMethod() [java] ==================================== [java] RUNNING POJO->throwsPOJOExceptionMethod("argument") [java] >>> afterThrowing: POJOException [java] >>> afterThrowingJoinPoint: POJOException [java] Catching Exception POJOException [java] [java] Calling POJO->throwRuntimeExceptionMethod() [java] ==================================== [java] RUNNING POJO->throwsRuntimeExceptionMethod("argument") [java] >>> afterThrowing: java.lang.ArrayIndexOutOfBoundsException [java] >>> afterThrowingArg: java.lang.ArrayIndexOutOfBoundsException [java] Catching Exception java.lang.ArrayIndexOutOfBoundsException [java] [java] Calling POJO->throwNullPointerExceptionMethod() [java] ==================================== [java] RUNNING POJO->throwsNullPointerExceptionMethod("argument") [java] >>> afterThrowing: java.lang.NullPointerException [java] >>> afterThrowingArg: java.lang.NullPointerException [java] >>> afterThrowingJoinPointArg: java.lang.NullPointerException [java] Catching Exception java.lang.NullPointerException [java] [java] Calling POJO->throwNothingMethod() [java] ================================== [java] RUNNING POJO->throwNothingMethod() [java] No Exception this time