异常处理思路
系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。
系统的dao、service、controller出现都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理,如下图:
springmvc提供全局异常处理器进行统一的异常处理,一个系统只有一个一个异常处理器
自定义异常类
对不同的异常类型定义异常类,继承Exception。
/**
* Created by Alex on 2017/6/29.
* 系统自定义异常类
*/
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;
}
}
全局异常处理器
思路:
系统遇到异常时,在程序中手动抛出,
dao抛给service,
service再抛给Controller,
最后Controller抛给前端控制器,
前端控制器调用全局异常处理器
全局异常处理器处理思路:
解析出异常类型,若该异常类型是系统自定义的异常,直接取出异常信息在错误页面展示即可。
如果不是系统自定义异常,构造一个系统自定义的异常类型,信息为“未知错误”
springmvc提供一个HandlerExceptopnResolver
/**
* Created by Alex on 2017/6/29.
* 全局异常处理器
*/
public class CustomExceptionResolver implements HandlerExceptionResolver{
/**
* 系统抛出的异常
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @param e 系统抛出的异常
* @return
*/
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
// 解析出异常类型
CustomException customException = null;
// 若该异常类型是系统自定义的异常,直接取出异常信息在错误页面展示即可。
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;
}
}
错误页面
<%--
Created by IntelliJ IDEA.
User: Alex
Date: 2017/6/29
Time: 20:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>失败!</title>
</head>
<body>
${message}
</body>
</html>
springmvc.xml中的配置全局异常处理器
<!--
全局异常处理器
只要类实现了HandlerExceptionResolver接口,就是一个全局异常处理器哦
-->
<bean class="com.alex.ssm.exception.CustomExceptionResolver"/>
异常测试
在controller,service,dao中任意一处需要手动抛出异常
如果是程序中手动抛出的异常,在错误页面中会显示自定义的异常信息,若不是手动抛出的异常,说明该异常是一个runtime异常,只显示“未知错误”。
在Controller里手动抛出异常
@RequestMapping(value = "/editItems",method = {RequestMethod.POST,RequestMethod.GET})
//@RequestParam 指定request传入的参数名,即可和形参进行绑定
// 通过required属性指定参数是否必须要传入
public String editItems(Model model, @RequestParam(value = "id",required = true)Integer items_id) throws Exception{
//调用service 根据商品ID查询商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
//判断商品是否为空,抛出异常
if(itemsCustom == null){
throw new CustomException("修改的商品不存在!");
}
//通过形参中的model将model的数据传到页面
//相当于modelandview的addObject
model.addAttribute("items",itemsCustom);
return "items/editItems";
}
在Service接口中手动抛出异常
@Override
public ItemsCustom findItemsById(Integer id) throws Exception {
Items items = itemsMapper.selectByPrimaryKey(id);
if(items == null){
throw new CustomException("修改的商品不存在!");
}
//对于商品信息进行业务处理
//....
//最终返回ItemsCustom
ItemsCustom itemsCustom = null;
//将items的内容拷贝到itemsCustom
if(items != null){
itemsCustom = new ItemsCustom();
BeanUtils.copyProperties(items,itemsCustom);
}
return itemsCustom;
}
如果与业务功能相关的信息,建议在service中抛出异常
与业务功能没有关系的信息,建议在Controller中抛出异常
上面的功能建议在service中抛出异常