异常处理

1.1      异常处理思路

系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。

         系统的dao、service、controller出现都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理,如下图:

 

springmvc提供全局异常处理器(一个系统只有一个异常处理器)进行统一异常处理。

1.2      自定义异常类

对不同的异常类型定义异常类,继承Exception。

 1 public class CustomException extends Exception {
 2     
 3     //异常信息
 4     public String message;
 5     
 6     public CustomException(String message){
 7         super(message);
 8         this.message = message;
 9     }
10 
11     public String getMessage() {
12         return message;
13     }
14 
15     public void setMessage(String message) {
16         this.message = message;
17     }
18 
19 }

1.3      全局异常处理器

思路:

         系统遇到异常,在程序中手动抛出,dao抛给service、service给controller、controller抛给前端控制器,前端控制器调用全局异常处理器。

         全局异常处理器处理思路:

                   解析出异常类型

                   如果该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展示

                   如果该 异常类型不是系统 自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)

springmvc提供一个HandlerExceptionResolver接口

 1 public class CustomExceptionResolver implements HandlerExceptionResolver {
 2 
 3     /**
 4      * (非 Javadoc)
 5      * <p>Title: resolveException</p>
 6      * <p>Description: </p>
 7      * @param request
 8      * @param response
 9      * @param handler
10      * @param ex 系统 抛出的异常
11      * @return
12      * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
13      */
14     @Override
15     public ModelAndView resolveException(HttpServletRequest request,
16             HttpServletResponse response, Object handler, Exception ex) {
17         //handler就是处理器适配器要执行Handler对象(只有method)
18         
19 //        解析出异常类型
20 //        如果该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展示
21 //        String message = null;
22 //        if(ex instanceof CustomException){
23 //            message = ((CustomException)ex).getMessage();
24 //        }else{
25 ////            如果该 异常类型不是系统 自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)
26 //            message="未知错误";
27 //        }
28         
29         //上边代码变为
30         CustomException customException = null;
31         if(ex instanceof CustomException){
32             customException = (CustomException)ex;
33         }else{
34             customException = new CustomException("未知错误");
35         }
36         
37         //错误信息
38         String message = customException.getMessage();
39         
40         
41         ModelAndView modelAndView = new ModelAndView();
42         
43         //将错误信息传到页面
44         modelAndView.addObject("message", message);
45         
46         //指向错误页面
47         modelAndView.setViewName("error");
48     
49         return modelAndView;
50     }
51 
52 }

1.4      错误页面

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>错误提示</title>
 8 </head>
 9 <body>
10 ${message }
11 </body>
12 </html>

1.5      在springmvc.xml配置全局异常处理器

1 <!-- 全局异常处理器
2     只要实现HandlerExceptionResolver接口就是全局异常处理器
3      -->
4     <bean class="xxx.xxxx.xxxx.CustomExceptionResolver"></bean>

1.6      异常测试

在controller、service、dao中任意一处需要手动抛出异常。

如果是程序中手动抛出的异常,在错误页面中显示自定义的异常信息,如果不是手动抛出异常说明是一个运行时异常,在错误页面只显示“未知错误”。

在商品修改的controller方法中抛出异常 .

 1 @RequestMapping(value = "/editItems", method = { RequestMethod.POST,
 2             RequestMethod.GET })
 3     // @RequestParam里边指定request传入参数名称和形参进行绑定。
 4     // 通过required属性指定参数是否必须要传入
 5     // 通过defaultValue可以设置默认值,如果id参数没有传入,将默认值和形参绑定。
 6     public String editItems(Model model,
 7             @RequestParam(value = "id", required = true) Integer items_id)
 8             throws Exception {
 9 
10         // 调用service根据商品id查询商品信息
11         ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
12         //判断商品是否为空,根据id没有查询到商品,抛出异常,提示用户商品信息不存 在
13         if(itemsCustom == null){
14             throw new CustomException("修改的商品信息不存在!");
15         }
16 
17         // 通过形参中的model将model数据传到页面
18         // 相当于modelAndView.addObject方法
19         model.addAttribute("items", itemsCustom);
20 
21         return "items/editItems";
22     }

在service接口中抛出异常:

 1     @Override
 2     public ItemsCustom findItemsById(Integer id) throws Exception {
 3         
 4         Items items = itemsMapper.selectByPrimaryKey(id);
 5         if(items==null){
 6             throw new CustomException("修改的商品信息不存在!");
 7         }
 8         //中间对商品信息进行业务处理
 9         //....
10         //返回ItemsCustom
11         ItemsCustom itemsCustom = null;
12         //将items的属性值拷贝到itemsCustom
13         if(items!=null){
14             itemsCustom = new ItemsCustom();
15             BeanUtils.copyProperties(items, itemsCustom);
16         }                
17         return itemsCustom;        
18     }

如果与业务功能相关的异常,建议在service中抛出异常。

与业务功能没有关系的异常,建议在controller中抛出。

上边的功能,建议在service中抛出异常。

原文地址:https://www.cnblogs.com/kingxiaozi/p/6058792.html