Spring MVC - Handling exceptions using HandlerExceptionResolver

[Updated: Feb 25, 2018, Created: Jan 17, 2017]

Spring MVC provides various mechanisms for error handling which are based on HandlerExceptionResolver interface.

Comparing with Servlet based error handling where we do mapping from 'http error code' and 'java exceptions' to servlets or pages, the interface HandlerExceptionResolver deals with 'java exceptions' only, that raised during controller execution. As we can programmatically take actions on exception scenarios, this approach is more flexible than web.xml mapping approach.

The interface HandlerExceptionResolver has only one method as shown in the following snippet:

package org.springframework.web.servlet;
public interface HandlerExceptionResolver {
	ModelAndView resolveException(HttpServletRequest request,
                                      HttpServletResponse response,
                                      Object handler,
                                      Exception ex);

The 'handler' parameter contains the Controller's handler method information where exception occurred. This parameter normally is the instance of HandlerMethod in Spring MVC. The method resolveException() will be invoked even if the exception is thrown outside of the target handler method, before or after handler's invocation but still in the course of target handler method invocation, for example in a handler interceptor.


In this example we are not going to use Spring Boot because Boot has additional default functionality regarding error handling. Here we are going to use traditional @EnableWebMvc approach to understand mvc only error handling aspect.

Creating and registering HandlerExceptionResolver implementation as a bean.

public class Config {

    HandlerExceptionResolver errorHandler () {
        return new HandlerExceptionResolver() {
            public ModelAndView resolveException (HttpServletRequest request,
                                                  HttpServletResponse response,
                                                  Object handler,
                                                  Exception ex) {
                ModelAndView model = new ModelAndView("error-page");
                model.addObject("exceptionType", ex);
                model.addObject("handlerMethod", handler);
                return model;

    //registering an interceptor
    public WebMvcConfigurer webMvcConfigurer () {
        return new WebMvcConfigurerAdapter() {
            public void addInterceptors (InterceptorRegistry registry) {
                registry.addInterceptor(new HandlerInterceptorAdapter() {
                    public boolean preHandle (HttpServletRequest request,
                                              HttpServletResponse response,
                                              Object handler) throws Exception {
                        if (request.getParameter("testParam") != null) {
                            throw new Exception("exception from interceptor");
                        return true;

    public ViewResolver viewResolver () {
        InternalResourceViewResolver viewResolver =
                  new InternalResourceViewResolver();
        return viewResolver;

    public ExampleController myMvcController () {
        return new ExampleController();

The Controller

public class ExampleController {

    public void handleRequest () {
        //just for testing
        throw new RuntimeException();

    public String testHandler () {
        return "test body";


<%@ page language="java"
    contentType="text/html; charset=ISO-8859-1"

<h3>This is example exception page</h3>
 <p>Exception: <b>${exceptionType}</b></p>
 <p>Handler method: <b> ${handlerMethod} </b></p>


Running the application

We are going to run the embedded tomcat server:

 mvn clean install tomcat7:run-war


Exception thrown in our HandlerInterceptor:

HandlerExceptionResolver.resolveException won't be invoked at the http status error e.g. 404 error

Example Project

Dependencies and Technologies Used:

  • spring-webmvc 4.3.5.RELEASE: Spring Web MVC.
  • javax.servlet-api 3.0.1 Java Servlet API
  • JDK 1.8
  • Maven 3.3.9

Handler Exception Resolver Example Select All Download
  • spring-mvc-exception-handling
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • webapp
          • WEB-INF
            • pages

See Also