Chapter 5. Capturing Join Points on Exception Handling

Introduction

This chapter shows how to include exception handling as part of the cross-cutting concerns you are applying to your application using aspects.

When an exception is thrown in Java, it is passed up the call chain until it is either handled by a catch statement as part of a try/catch block or it reaches the Java run-time and causes a messy message on your console. If a Java exception is caught then the exception is passed as an object to the corresponding catch block where the appropriate action can take place to handle the problem.

The aspects you are applying to your applications may find it useful to know when an exception has been handled. It is quite possible that part of the cross-cutting behavior you are implementing, using aspects, requires something to be done in addition to, or instead of, the normal behavior of a catch block.

AspectJ provides the handler(TypePattern) pointcut for use when you want to capture when a catch block has been invoked with a particular type of exception. This chapter shows you some of the ways to use the handler(TypePattern) in your aspects so that they can capture and interact with the exceptions that can be raised by your target application.

5.1. Capturing When an Exception Is Caught

Problem

You want to capture when a particular type of exception is caught.

Solution

Use the handler(TypePattern) pointcut. The syntax of the handler(TypePattern) pointcut is:

pointcut <pointcut name>( ...

Get AspectJ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.