Spring 3 Controller Exception handler implementation problems

Posted by predhme on Stack Overflow See other posts from Stack Overflow or by predhme
Published on 2010-04-13T21:45:23Z Indexed on 2010/04/13 22:23 UTC
Read the original article Hit count: 785

I was hoping to implement a single "ExceptionController" to handle exceptions that are thrown in execution of my other controllers' methods. I hadn't specified any HandlerExceptionResolver in my application context so according to the API documentation the AnnotationMethodHandlerExceptionResolver should be started. Verified as such in the source. So I am curious to know why the following isn't working.

@Controller
public class ExceptionController {

  @ExceptionHandler(NullPointerException.class)
  public ModelAndView handleNullPointerException(NullPointerException ex) {
    // Do some stuff
    log.error(logging stuff)
    return myModelAndView;
  }
}

@Controller
public class AnotherController {

  @RequestMapping(value="/nullpointerpath")
  public String throwNullPointer() {
    throw new NullPointerException();
  }
}

I see in the debug logs that the three default exception handlers are asked for handling of the exception, but nothing is done and I see "DispatcherServlet - Could not complete request". Followed by the user being displayed the stacktrace and a 500 Internal error.

© Stack Overflow or respective owner

Related posts about spring-mvc

Related posts about java