Ya, basically the question. In a few of my handlers I have a @ModelAttribute, in an extreme edge case that @ModelAttribute can throw an excetion. When that happens I want to display an error page. I want to do this all in the @ModelAttribute method as I want to avoid redundant code in the different request handlers.
1 Answer
Have you tried @ExceptionHandler?
@ExceptionHandler(YourException.class)
public ModelAndView handleException(YourException e, HttpServletRequest request) {
// Handle it
return yourView;
}
-
no problem - most of the annotations that work on a RequestMapping should work on an ExceptionHandler too...– MattRJul 6, 2012 at 13:27
-
Note: the @ ExceptionHandler only applies to the methods of the class in which it appears. If you have multiple classes, you will need to supply an @ ExceptionHandler in each class. Jul 6, 2012 at 18:54