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 {
logger.info("This will throw a NoSuchRequestHandlingMethodException, which is Spring's 404 not found");
boolean throwException = true;
if (throwException) {
throw new NoSuchRequestHandlingMethodException(
"This is my NoSuchRequestHandlingMethodException",
this.getClass());
}
return "home";
}
@RequestMapping(value = "/nullpointer", method = RequestMethod.GET)
public String throwNullPointerException(Locale locale, Model model)
throws NoSuchRequestHandlingMethodException {
logger.info("This will throw a NullPointerException");
String str = null; // Ensure that this is null.
str.length();
return "home";
}
… then I can catch them both by writing an exception handler that looks like this:
@ExceptionHandler({ NullPointerException.class,
NoSuchRequestHandlingMethodException.class })
public ModelAndView handleExceptionArray(Exception ex) {
logger.info("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();
modelAndView.setViewName("error");
modelAndView.addObject("name", ex.getClass().getSimpleName());
modelAndView.addObject("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(AnnotationMethodHandlerExceptionResolver.java:241) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.doResolveException(AnnotationMethodHandlerExceptionResolver.java:128) at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:136) at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:987) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:811) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
...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....
- The full webapp sample is available at:
git://github.com/roghughe/captaindebug.git
- See the Spring documentation for reference material.
No comments:
Post a comment