@ExceptionHandler in Spring

model-view-controller, spring

Solution

Throw the exception again which will be handled by DefaultHandlerExceptionResolver to respond with the error page defined in your web.xml; It doesn't invoke the same Exception Handler of the controller.

@ExceptionHandler(Exception.class)
public ModelAndView generateException(Exception ex) throws Exception{
    if(condition) {
        return new ModelAndView("myError.jsp");
    } else {
        //should execute default error page.
        throw ex;
    }   
}

Problem

In my application I have configured an error page in web.xml. I need to override that for a specific controller based on a condition. Here when a condition becomes true I need to redirect to a specific error page otherwise normal error page should be rendered. Here is the code. Please help me. ``` @Controller public class Test{ @ExceptionHandler(Exception.class) public ModelAndView generateException(HttpServletRequest httpServletRequest){ if(condition) { return new ModelAndView("myError.jsp"); } else { //should execute default error page. } } } ```

Original source