1.
@ExceptionHandler(ArithmeticException.class)
public ModelAndView getArithmeticException(Exception ex){
ModelAndView mv = new ModelAndView("error");
mv.addObject("ex", ex);
return mv;
}
@RequestMapping("/zero")
public void ac(@RequestParam("i") int i){
System.out.println(10/i);
}
当发生ArithmeticException异常的时候,在error.jsp页面输出异常 。
2.
@ControllerAdvice
public class Exceptions {
@ExceptionHandler(ArithmeticException.class)
public ModelAndView getArithmeticException(Exception ex){
ModelAndView mv = new ModelAndView("error");
mv.addObject("ex", ex);
return mv;
}
}
如果在本类中找不到异常处理的方法,就去@ControllerAdvice注解的类中查找异常处理的类的方法。
3.
@ResponseStatus(value=HttpStatus.BAD_REQUEST,reason="请求不对")
public class UserExceptions extends RuntimeException{
/**
*
*/
private static final long serialVersionUID = 1L;
}
在controller的方法里抛出UserExceptions 异常,在页面上显示
HTTP Status 400 - 请求不对