Recipe5.2.Capturing the Thrown Exception


Recipe 5.2. Capturing the Thrown Exception

Problem

Within the advice triggered by a join point captured using the handler(TypePattern) pointcut, you want to access the exception that was being caught by the catch block within the corresponding advice.

Solution

Combine the args([Types | Identifiers]) pointcut with the handler(TypePattern) pointcut to expose the caught exception as an identifier on your pointcut that can be passed to the corresponding advice.

Discussion

Example 5-2 shows how the MyException exception is passed to the before( ) advice as the exception identifier on the myExceptionHandlerPointcut pointcut.

Example 5-2. Accessing the caught MyException exception
public aspect AccessThrownException  {    pointcut myExceptionHandlerPointcut(MyException exception) :        handler(MyException) &&        args(exception);    // Advice declaration    before(MyException exception) : myExceptionHandlerPointcut(exception)    {       System.out.println(          "-------------- Aspect Advice Logic ---------------");       System.out.println(          "In the advice picked by " + "myExceptionHandlerPointcut( )");       System.out.println(          "Signature: "             + thisJoinPoint.getStaticPart( ).getSignature( ));       System.out.println(          "Source Line: "             + thisJoinPoint.getStaticPart( ).getSourceLocation( ));              System.out.println("Exception caught:");       exception.printStackTrace( );              System.out.println(          "--------------------------------------------------");    } }

See Also

The handler(TypePattern) poincut's syntax is shown in Recipe 5.1; Recipe 5.1 also shows some of the wildcard variations that can be used in a TypePattern; the args([Types | Identifiers]) pointcut declaration is explained in Recipe 11.3; Chapter 13 describes the different types of advice available in AspectJ.



AspectJ Cookbook
Aspectj Cookbook
ISBN: 0596006543
EAN: 2147483647
Year: 2006
Pages: 203
Authors: Russ Miles

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net