try catch 异常捕获处理
我们在写代码时喜欢用try catch来捕获异常并且很好用。
在我们在项目中是,会遇到Controller层无法捕获异常。回看代码。发现Service层也使用了try catch 。
因此我们做了一些统一。把service异常全部抛出到Controller层做捕获处理。
接口:
public void updateTest ( AddrsbEmpDTO list)throws Exception;
实现:
public void updateTest(Test list) throws Exception{ dao.update(list); }
Controller
@RequestMapping(params = { "doUpdate" }) @ResponseBody public AjaxJson doUpdate(AddrsbDepartmentDTO entity, HttpServletRequest request, HttpServletResponse response) { AjaxJson j = new AjaxJson(); try { testServerce.updateTset( entity ); }catch (Exception e) { j.setMsg(message); j.setSuccess(false); logger.error("更新数据失败【" + entity.getComCode() + "】", e); } return j; }
也可在实现类中使用try catch捕获 做些独特的处理
public void updateTest(Test list) throws Exception{
try {dao.update(list);
}catch (Exception e) {
独特处理;
throw e;
}}但是一定要throw e;