SpringMVC_异常处理
来源:互联网 发布:网络用语 简写 编辑:程序博客网 时间:2024/04/29 08:29
1 异常处理思路:
系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过补货异常从而获取异常信息,后者主要通过规范代码开发,测试通过手段减少运行时异常的发生
系统的dao service controller出现都通过throws Exception向上抛出,最后由springMVC前端控制器交由异常处理器进行异常处理:
2 自定义异常类:
对不同的异常类型定义异常类,继承Exception
3 全异常处理器
思路:
系统遇到异常,在程序中手动抛出,dao抛给service service抛给controller,controller抛给前端控制器,前端控制器调用全局异常处理器
全局异常处理器处理思路:
解析出异常类型
如果该异常类型是系统自定义的异常 直接取出异常信息 错误页面展示
如果该异常类型不是系统自定义的异常,构造一个自定义的异常类型(信息为“位置错误”)
springMVC提供一个HandlerExceptionresolver接口
@Overridepublic ModelAndView resolveException(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex) {//handler就是处理器适配器要执行Handler对象(只有method)//解析出异常类型//如果该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展示//String message = null;//if(ex instanceof CustomException){//message = ((CustomException)ex).getMessage();//}else{////如果该 异常类型不是系统 自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)//message="未知错误";//}//上边代码变为CustomException customException = null;if(ex instanceof CustomException){customException = (CustomException)ex;}else{customException = new CustomException("未知错误");}//错误信息String message = customException.getMessage();ModelAndView modelAndView = new ModelAndView();//将错误信息传到页面modelAndView.addObject("message", message);//指向错误页面modelAndView.setViewName("error");return modelAndView;}
4 springmvc.xml配置全局异常处理器
1 0
- SpringMVC_异常处理
- SpringMVC_统一异常处理
- 6.SpringMVC_处理模型数据
- springMVC_处理不经过DispatcherServlet的文件
- 14. SpringMVC_处理模型数据之ModelAndView
- 15 SpringMVC_处理模型数据之Map
- 16. SpringMVC_处理模型数据之 SessionAttributes 注解
- SpringMVC_使用
- SpringMVC_使用
- SpringMVC_使用
- SpringMVC_使用
- springMVC_国际化
- SpringMVC_使用
- SpringMVC_使用
- SpringMVC_使用
- SpringMVC_使用
- SpringMVC_开天辟地
- SpringMVC_@RequestBody
- SYBASE全库备份方案
- MySQL数据类型注意事项
- page、request、session和application有什么区别?
- C-038.const关键字
- 微信收费事件背后被广泛忽略的技术细节——长连接心跳周期
- SpringMVC_异常处理
- POJ 1410 Intersection
- HTTP Status 500 - Error instantiating servlet class com.*.servlet.TestServlet
- Graph Based Algorithms, why 2-D instead of 3-D ?
- 安卓Activity--Activity的跳转动画
- Pig Hive对比
- android屏幕适配
- Oracle中In函数的使用
- InputStream类中read(byte[])和read(byte, int offset, int length)