java学习

java学习

 

springmvc异常处理方式1

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 - 请求不对

posted on 2016-07-28 09:49 杨军威 阅读(101) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜