In general, you can catch an exception in Spring MVC as follows:
@ExceptionHandler(Exception.class) public ModelAndView handleMyException(Exception exception) { ModelAndView modelAndView = new ModelAndView("/errors/404"); modelAndView.addObject("message", exception.getMessage()); return modelAndView; }
You can match it with any exception time and redirect the user to any page with any messages.
Alternatively: you can return it to @ResponseBody :
@ExceptionHandler(Exception.class) @ResponseBody public String handleMyException(Exception exception) { return exception.getMessage(); }
source share