Spring MVC异常处理机制详解.pdfVIP

  • 0
  • 0
  • 约8.9千字
  • 约 10页
  • 2026-03-11 发布于北京
  • 举报

参考资料一

《注解篇》

⚫全录

异常捕获注解

/**

*1.在@ExceptionHandler方法的入参中可以加入Exception类型的参数,该参数即对应发生

的异常对象*2.@ExceptionHandler方法的入参中不能传入Map。若希望把异常信息传导页面上,

需要使用ModelAndView作为返回值*3.@ExceptionHandler方法标记的异常有优先级的问题。

*4.@ControllerAdvice:如果在当前Handler中找不到@ExceptionHandler方法来处理当前方法

出现的异常,*则将去@ControllerAdvice标记的类中查找@ExceptionHandler标记的方法来处

理异常。*/@ExceptionHandler({ArithmeticException.class})publicModelAndView

handleArithmeticException(Exceptionex){System.out.println(出异常了:+ex);

ModelAndViewmv=newModelAndView(error);mv.addObject(exception,ex);return

mv;}

在Controller中当方法抛出异常而没有进行处理时,将会查找当前Controller中具有

@ExceptionHandler注解的方法,此方式具有优先级,越准确优先级越

参考资料二

《异常处理》这篇参考

了《异常处理》这篇中说了

一些神奇的用法,我都没有用过。

我的实验项目

error.rar

参考资料一

《注解篇》

⚫全录

异常捕获注解

/**

*1.在@ExceptionHandler方法的入参中可以加入Exception类型的参数,该参数即对应发生

的异常对象

*2.@ExceptionHandler方法的入参中不能传入Map.若希望把异常信息传导页面上,需要使用

ModelAndView作为返回值

*3.@ExceptionHandler方法标记的异常有优先级的问题.

*4.@ControllerAdvice:如果在当前Handler中找不到@ExceptionHandler方法来出来当前

方法出现的异常,

*则将去@ControllerAdvice标记的类中查找@ExceptionHandler标记的方法来处理异常.

*/

@ExceptionHandler({ArithmeticException.class})

publicModelAndViewhandleArithmeticException(Exceptionex){

System.out.println(出异常了:+ex);

ModelAndViewmv=newModelAndView(error);

mv.addObject(exception,ex);

returnmv;

}

在Conrolle中当方法错如果没有进行ycatch将会查找当前conoe中具有

@ExceponHande注解的方法,此方式具有优先级,越准确优先级越

参考资料二

《异常处理》

这篇参考了《异常处理》

这篇中说了一些神奇的用法,我都没有用过。

我的实验项目

error.rar

前言说明

非SpringBoot时代和SpringBoot时代分别比较赞成的支持方式。

办法非springboot方式使用SpringBoot方式用

1error.html不好,这是springboot可以

4xx.html,5xx.html,

404.html,500.html

2web.xml中指定HTTP错误码(404、500)应当再次的路径可以可能有办法做到

3@ExceptionHandler可以

文档评论(0)

1亿VIP精品文档

相关文档