標籤:primary 適配器 包括 lang dao 繼承 拋出異常 ice encoding
一、異常處理思路:
系統中異常包括兩類:預期異常和運行時異常RuntimeException,前者通過捕獲異常從而擷取異常資訊,後者主要通過規範代碼開發、測試通過手段減少運行時異常的發生。
系統的dao、service、controller出現都通過throws Exception向上拋出,最後由springmvc前端控制器交由異常處理器進行異常處理,如:
springmvc提供全域異常處理器(一個系統只有一個異常處理器)進行統一異常處理。
二、自訂異常類 繼承Exception
1 package com.cy.exception; 2 3 /** 4 * 系統 自訂異常類,針對預期的異常,需要在程式中拋出此類的異常 5 * @author chengyu 6 * 7 */ 8 public class CustomException extends Exception{ 9 10 public String message; 11 12 public CustomException(String message){13 super(message);14 this.message = message;15 }16 17 public String getMessage() {18 return message;19 }20 21 public void setMessage(String message) {22 this.message = message;23 }24 }CustomException
三、全域異常處理器:
思路:
系統遇到異常,在程式中手動拋出,dao拋給service、service給controller、controller拋給前端控制器,前端控制器調用全域異常處理器。
全域異常處理器處理思路:
解析出異常類型
如果該 異常類型是系統 自訂的異常,直接取出異常資訊,在錯誤頁面展示
如果該 異常類型不是系統 自訂的異常,構造一個自訂的異常類型(資訊為“未知錯誤”)
1 package com.cy.exception; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.HandlerExceptionResolver; 7 import org.springframework.web.servlet.ModelAndView; 8 9 /**10 * 全域異常處理器11 * @author chengyu12 *13 */14 public class CustomExceptionResolver implements HandlerExceptionResolver{15 16 /**17 * ex 系統 拋出的異常18 * handler就是處理器適配器要執行Handler對象(只有method)19 */20 @Override21 public ModelAndView resolveException(HttpServletRequest request,22 HttpServletResponse response, Object handler, Exception ex) {23 24 CustomException customException = null;25 26 //解析出異常類型27 //如果該 異常類型是系統 自訂的異常,直接取出異常資訊,在錯誤頁面展示28 //如果該 異常類型不是系統 自訂的異常,構造一個自訂的異常類型(資訊為“未知錯誤”)29 if(ex instanceof CustomException){30 customException = (CustomException) ex;31 }else{32 customException = new CustomException("未知錯誤!");33 }34 35 ModelAndView modelAndView = new ModelAndView();36 modelAndView.addObject("message", customException.getMessage());37 modelAndView.setViewName("error");38 return modelAndView;39 }40 41 }View Code
四、錯誤頁面:
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>View Code
在springmvc.xml中配置全域異常處理器:
1 <!-- 全域異常處理器 只要實現HandlerExceptionResolver介面就是全域異常處理器 -->2 <bean class="com.cy.exception.CustomExceptionResolver" />
View Code
五、測試代碼:
在controller、service、dao中任意一處手動拋出異常;手動拋出的異常,在錯誤頁面顯示自訂的錯誤異常資訊;
如果不是手動拋出的異常,說明是一個RunTimeException;在錯誤頁面只顯示“未知錯誤”;
1)在Controller中拋出異常:
1 @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET}) 2 public String editItems(Model model,@RequestParam(value="id",required=true,defaultValue="4") Integer items_id) throws Exception{ 3 ItemsCustom itemsCustom = itemsService.findItemsById(items_id); 4 5 if(itemsCustom == null){ 6 throw new CustomException("商品資訊不存在!"); 7 } 8 9 model.addAttribute("items", itemsCustom);10 11 return "items/editItems";12 }View Code
2)在service中拋出異常:
1 @Override 2 public ItemsCustom findItemsById(Integer id) throws Exception { 3 Items items = itemsMapper.selectByPrimaryKey(id); 4 5 if(items == null){ 6 throw new CustomException("該商品資訊不存在啊!"); 7 } 8 9 ItemsCustom itemsCoustom = null;10 if(items!=null){11 itemsCoustom = new ItemsCustom();12 BeanUtils.copyProperties(items, itemsCoustom);13 }14 15 return itemsCoustom;16 }View Code
程式運行到拋出異常會跳出該方法的,不再執行;
如果與業務功能相關的異常,建議在service中拋出異常。
與業務功能沒有關係的異常,建議在controller中拋出。
上邊的功能,建議在service中拋出異常。
springMVC學習(9)-全域異常處理