1. @Controller level
Annotate a separate method in your @Controller as a @ExceptionHandler
simply catch the Exception yourself in your handler method
2. DispatcherServlet level
Rely on the DefaultHandlerExceptionResolver:Maps common exceptions to appropriate status codes
Supplement with your own custom HandlerExceptionResolver as needed
@ExceptionHandler
public @ResponseBody String handle(IllegalStateException e) {
return "IllegalStateException handled!";
}
@RequestMapping("/exception")
public @ResponseBody String exception() {
throw new IllegalStateException("Sorry!");
}