[springmvc] Exception Handling

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!");
}



posted on 2012-03-26 23:45  leaving  阅读(255)  评论(0)    收藏  举报