System.out.println("IllegalArgumentException Capture");
System.out.println("Caught: " + ex.getClass().getName());
System.out.println("Method: " + method.getName());
System.out.println("***\n");
}
}
We are sure that you understand the code in the main() method, so now we will just focus on the two
afterThrowing() methods. The first thing Spring looks for in a throws advice is one or more public methods
called afterThrowing(). The return type of the methods is unimportant, although we find it best to stick
with void because this method can't return any meaningful value. The first afterThrowing() method in the
SimpleThrowsAdvice class has a single argument of type Exception. You can specify any type of Exception as
the argument, and this method is ideal when you are not concerned about the method that threw the
exception or the arguments that were passed to it. Note that this method catches Exception and any
subtypes of Exception unless the type in question has its own afterThrowing() method.
In the second afterThrowing() method, we declared four arguments to catch the Method that threw
the exception, the arguments that were passed to the method, and the target of the Method Invocation.
The order of the arguments in this method is important, and you must specify all four. Notice that the
second afterThrowing() method catches exceptions of type IllegalArgumentException (or its subtype).
Running this example produces the following output:
***
Generic Exception Capture
Caught: java.lang.Exception
***
***
IllegalArgumentException Capture
Caught: java.lang.IllegalArgumentException
Method: otherErrorProneMethod
***
As you can see, when a plain old Exception is thrown, the first afterThrowing() method is invoked,
but when an IllegalArgumentException is thrown, the second afterThrowing() method is invoked. Spring
invokes a single afterThrowing() method only for each Exception, and as you saw from the example in
Listing 6-18, Spring uses the method whose signature contains the best match for the Exception type. In
the situation where your after-throwing advice has two afterThrowing() methods, both declared with the
same Exception type but one with a single argument and the other with four arguments, Spring invokes
the four-argument afterThrowing() method.
As we mentioned earlier, after-throwing advice is useful in a variety of situations; it allows you to
reclassify entire Exception hierarchies as well as build centralized Exception logging for your application.
We have found that after-throwing advice is particularly useful when we are debugging a live application,
because it allows us to add extra logging code without needing to modify the application's code.
Choosing an Advice Type
In general, the choice of which advice type you want to use is driven by the requirements of your
application, but you should choose the most specific advice type for your need. That is to say, don't use an
around advice when a before advice will do. In most cases, an around advice can accomplish everything
that the other three advice types can, but it may be overkill for what you are trying to achieve. By using the
most specific type of advice, you are making the intention of your code clearer, and you are also reducing
the possibility of errors. Consider an advice that counts method calls. When you are using before advice, all
you need to code is the counter, but with an around advice, you need to remember to invoke the method
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home