I used an ExceptionHandler to throw catch in controllers.
@ExceptionHandler(value = {Exception.class, RuntimeException.class}) public final ModelAndView globalExceptionHandler(final Exception exception) { ModelAndView modelAndView = new ModelAndView("error/500"); modelAndView.addObject("tl_exception", errorSystem.processingError(exception)); return modelAndView; }
But, for example, if in the jsp file I am tring to get data from a null object, this exception is not cathcing.
I need advice, how can I catch an exception in a jsp file? Or all the errors that I need to catch only in the controllers?
Update:
The best solution is placed in the url for web.xml for errors.
<error-page> <location>/error</location> </error-page>
After creating the controller, which is necessary to handle the error from the request:
@Controller public final class ErrorController { @RequestMapping(value = "/error") public final ModelAndView globalErrorHandle(final HttpServletRequest request) { String page = "error/500"; final String code = request.getAttribute("javax.servlet.error.status_code").toString(); if (null != code && !code.isEmpty()) { final Integer statusCode = Integer.parseInt(code); switch (statusCode) { case 404 : page = "error/404"; case 403 : page = "error/403"; } } return new modelAndView(page); } }
source share