SpringMVC 异常的处理

来源:互联网 发布:淘宝零销量能买吗 编辑:程序博客网 时间:2024/06/05 04:10
1.处理局部异常(Controller内)

@ExceptionHandler 

public ModelAndView exceptionHandler(Exception ex){ 

ModelAndView mv = new ModelAndView("error"); 

mv.addObject("exception", ex); 

System.out.println("in testExceptionHandler"); return mv;

 } 

@RequestMapping("/error") 

public String error(){ 

int i = 5/0; return "hello"; 

}

2.处理全局异常(所有Controller)

@ControllerAdvice 

public class testControllerAdvice { 

@ExceptionHandler 

public ModelAndView exceptionHandler(Exception ex)

ModelAndView mv = new ModelAndView("error"); 

mv.addObject("exception", ex); 

System.out.println("in testControllerAdvice"); 

return mv; 

}

0 0
原创粉丝点击