When I use @ExceptionHandler within Controller, it's not working as expected.
Here is my code:
@Controller public class PageController { @RequestMapping("/page") public ModelAndView index(ModelAndView modelAndView){ String mess = null; mess.split(","); modelAndView.setViewName("index"); return modelAndView; } @ExceptionHandler({Exception.class}) @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Bad Request") public ModelAndView handleException(Exception ex, HttpServletRequest request, HttpServletResponse response){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message", ex.getMessage()); modelAndView.addObject("url", request.getRequestURL()); modelAndView.addObject("code", response.getStatus()); modelAndView.setViewName("exception"); return modelAndView; } } After the application starts with debug mode, I visit http://localhost:8080/page, and handleException is running, but the view is below other than the expected excepction view. Why?
