Recipe 5.1. Capturing When an Exception Is CaughtProblemYou want to capture when a particular type of exception is caught. SolutionUse the handler(TypePattern) pointcut. The syntax of the handler(TypePattern) pointcut is: pointcut <pointcut name>(<any values to be picked up>) : handler(<class>); DiscussionThe handler(TypePattern) pointcut has five key characteristics:
Table 5-1 shows some examples of the wildcard options available when using a TypePattern to a pointcut declaration.
Example 5-1 shows the handler(TypePattern) pointcut to capture a MyException exception being caught. Example 5-1. Using the handler(TypePattern) pointcut to capture join points when a specific type of exception is caughtpublic aspect HandlerRecipe { /* Specifies calling advice when any exception object is caught that matches the following rules for its type pattern: Type: MyException */ pointcut myExceptionHandlerPointcut( ) : handler(MyException); // Advice declaration before( ) : myExceptionHandlerPointcut( ) { 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( "--------------------------------------------------"); } }
Figure 5-1 shows how the handler(TypePattern) pointcut is applied to a simple class hierarchy. Figure 5-1. How the handler(TypePattern) pointcut is appliedSee AlsoChapter 13 describes the different types of advice available in AspectJ. |