Spring MVC ---- Exception Handling

Controller

Mode 1;

    // handle exceptions, noted in the reference, can not have Model, Map, etc., if desired to add an object requestion, using ModelAndView 
    @ExceptionHandler (ArithmeticException.class {}) 
    public ModelAndView an ArithmeticException (Exception EX) { 
        ModelAndView ModelAndView new new ModelAndView = ( " error "); 
        modelAndView.addObject (" EX ", EX); 
        return ModelAndView; 
    } 
    @RequestMapping (value =" / testexception ", Method = RequestMethod.GET) 
    public String testexception () { 
        int A = 1/0; 
        return" Login "; 
    }

Option 2: 

We re-create a class (class name has nothing to do), the above method to handle exceptions into them;

@ControllerAdvice
public class HandleException {
    @ExceptionHandler({ArithmeticException.class})
    public ModelAndView ArithmeticException(Exception ex){
        ModelAndView modelAndView = new ModelAndView("error");
        modelAndView.addObject("ex",ex);
        return modelAndView;
    }
}

  

Guess you like

Origin www.cnblogs.com/yanxiaoge/p/11285869.html