Thursday, 9 February 2012

Spring 3 MVC Exception Handlers and Multiple Exception Arrays

My last blog was the first in a short series of blogs examining Spring 3 MVC’s exception handling annotations. It covered the basic usage of the @ExceptionHandler annotation with a few pieces of demo code and no contrived scenarios. Today’s blog continues where I left off and examines @ExceptionHandler in more detail.

You may recall that my last blog demonstrated1 that the @ExceptionHandler obeys the usual rules of exception handling in that any annotated method would catch the specified exception and its subclasses. My demo code caught IOException, which means that ChangedCharSetException, CharacterCodingException, CharConversionException, ClosedChannelException, EOFException,FileLockInterruptionException, FileNotFoundException, FilerException, etc. etc... would also be caught, if thrown by the sample application.

So, what happens if I want to catch two or more unrelated exceptions? This is achieved by specifying @ExceptionHandler with an array of exception classes. For example, given the two flaky controller methods below, one throwing NullPointerException and the other throwing NoSuchRequestHandlingMethodException...

  @RequestMapping(value = "/my404", method = RequestMethod.GET)
public String throwNoSuchRequestHandlingMethodException(Locale locale,
      Model model
) throws NoSuchRequestHandlingMethodException {"This will throw a NoSuchRequestHandlingMethodException, which is Spring's 404 not found");

boolean throwException = true;

if (throwException) {
throw new NoSuchRequestHandlingMethodException(
"This is my NoSuchRequestHandlingMethodException",

return "home";

@RequestMapping(value = "/nullpointer", method = RequestMethod.GET)
public String throwNullPointerException(Locale locale, Model model)
throws NoSuchRequestHandlingMethodException {"This will throw a NullPointerException");

    String str =
null; // Ensure that this is null.

return "home";

… then I can catch them both by writing an exception handler that looks like this:

  @ExceptionHandler({ NullPointerException.class,
class })
public ModelAndView handleExceptionArray(Exception ex) {"handleExceptionArray - Catching: "
+ ex.getClass().getSimpleName());
return errorModelAndView(ex);
   * Get the users details for the 'personal' page
private ModelAndView errorModelAndView(Exception ex) {
ModelAndView modelAndView = new ModelAndView();
("name", ex.getClass().getSimpleName());
("user", userDao.readUserName());

return modelAndView;

There is a slight GOTCHA to avoid here. If you choose to pass the exception in as an argument as I have, then you need to ensure that it’s a super class of all the exceptions in the array. In the above case I’ve used a straight forward exception class:

  public ModelAndView handleExceptionArray(Exception ex)

If you get this wrong, then you won’t know about it until run-time when you’ll see the following exception in your server’s log file:

ERROR: org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Invoking request method resulted in exception : public org.springframework.web.servlet.ModelAndView com.captaindebug.exceptions.ExceptionsDemoController.handleExceptionArray(java.lang.NullPointerException)
java.lang.IllegalStateException: Unsupported argument [java.lang.NullPointerException] for @ExceptionHandler method: public org.springframework.web.servlet.ModelAndView com.captaindebug.exceptions.ExceptionsDemoController.handleExceptionArray(java.lang.NullPointerException)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.resolveHandlerArguments(
 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.doResolveException(
 at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(
 at org.springframework.web.servlet.DispatcherServlet.processHandlerException(
 at org.springframework.web.servlet.DispatcherServlet.doDispatch(
 at org.springframework.web.servlet.DispatcherServlet.doService(
 at org.springframework.web.servlet.FrameworkServlet.processRequest(
 at org.springframework.web.servlet.FrameworkServlet.doGet(

...which may or may not be caught in testing as it's easily missed unless you check for every exception.

There are another couple of points to cover when it comes to Spring 3 MVC exception handling including the use of the @ResponseStatus annotation, but more on that later....

  1. The full webapp sample is available at:

  2. See the Spring documentation for reference material.

No comments: