异常处理

作者: 常威爆打来福 | 来源:发表于2018-05-18 12:01 被阅读5次
1.系统异常

(1) 预期异常
通过捕获异常从而获取异常信息
(2) 运行时异常RuntimeException
主要通过规范代码开发、测试通过手段减少运行时异常的发生。

2.异常处理思路

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


异常处理流程
3.自定义异常类
  • 对不同的异常类型定义异常类(有多少异常类型就定义多少异常类),继承Exception
package com.exception;

/**
 * 系统自定义异常类
 * @author:Yang
 * @date:2018/5/18
 */
public class CustomException extends Exception {
    /**
     * 异常信息
     */
    public String message;
    public CustomException(String message){
        super(message);
        this.message = message;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

4.全局异常处理器

(1)思路:
系统遇到异常,在程序中手动抛出,dao抛给service、service给controller、controller抛给前端控制器,前端控制器调用全局异常处理器。
(2)解析出异常类型
如果该异常类型是系统自定义的异常,直接取出异常信息,在错误页面展示
如果该异常类型不是系统自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)

package com.exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常处理器
 * @author:Yang
 * @date:2018/5/18
 */
public class CustomExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest,
                                         HttpServletResponse httpServletResponse,
                                         Object o, Exception e) {
        //handler就是处理器适配器要执行Handler对象(只有method)
        //解析出异常类型
        //如果该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展示
        CustomException customException;
        if (e instanceof CustomException){
            customException =(CustomException)e;
        }else{
            //如果该 异常类型不是系统 自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)
            customException = new CustomException("未知错误");
        }
        String message = customException.getMessage();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message",message);
        modelAndView.setViewName("error");
        return modelAndView;
    }
}

在springMVC.xml中配置全局异常处理器

    <!--全局异常处理器,实现HandlerExceptionResolver接口就是全局异常处理器-->
    <bean class="com.exception.CustomExceptionResolver"></bean>

异常显示页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${message}
</body>
</html>
5.测试

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

如果与业务功能相关的异常,建议在service中抛出异常。
例如:查询的商品为空
与业务功能没有关系的异常,建议在controller中抛出。
例如:验证文本内容不能出现特殊字符

测试
    @RequestMapping("/editItems")
    public String editItems(Model model, @RequestParam(value = "id", required = true, defaultValue = "1") Integer items_id) throws Exception {
        ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
        if (itemsCustom == null){
            throw new CustomException("商品信息不存在");
        }
        model.addAttribute("itemsCustom", itemsCustom);
        return "editItems";
    }
未知错误
原因
出现未知错误原因

查询不在id导致items为空,无法正常复制对象给itemsCustom,导致出现未定义异常,即“未知异常”

解决
......省略
        ItemsCustom itemsCustom = null;
        //将items的属性拷贝到itemsCustom
        if(items != null){
            itemsCustom = new ItemsCustom();
            BeanUtils.copyProperties(items,itemsCustom);
        }
.....省略
成功

相关文章

网友评论

    本文标题:异常处理

    本文链接:https://www.haomeiwen.com/subject/czyidftx.html