在common工程创建捕获异常的类:CustomException

Runtime叫做运行异常。在代码中抛出的话 对我们的代码没有可侵入性

如果在代码上抛出

如果改成Exception 这时候就会有错误提示。

那就必须要在方法的上面抛出‘

要么不在方法上抛出,去捕获

指定resultCode使用构造方法来赋值

捕获到了错误代码。就需要一个get方法去取出这个错误代码。这样自定义 异常类型就定义好了。

public class CustomException extends RuntimeException {
ResultCode resultCode; public CustomException(ResultCode resultCode){
this.resultCode=resultCode;
} public ResultCode getResultCode() {
return resultCode;
}
}

定义好了自定义异常类,这里我们就可以在Service内 这么去抛出异常

 public CmsPageResult add(CmsPage cmsPage){
//校验页面名称、站点Id、页面WebPath的唯一性
CmsPage cmsPage1=cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(),cmsPage.getSiteId(),cmsPage.getPageWebPath());
if(cmsPage1==null){
throw new
CustomException(CommonCode.FAIL);
}

cmsPage.setPageId(null);//设置设置为null 让mongoDB自动去生成,
cmsPageRepository.save(cmsPage);
return new CmsPageResult(CommonCode.SUCCESS,cmsPage);
//return new CmsPageResult(CommonCode.FAIL,null);
}

再专门定义一个异常抛出类

ExceptionCast

定义静态的方法,

public class ExceptionCast {
public static void cast(ResultCode resultCode){
throw new CustomException(resultCode);
}
}

这样抛出异常。这种写法 就方便很多,

if(cmsPage1==null){
//throw new CustomException(CommonCode.FAIL);
ExceptionCast.cast(CommonCode.FAIL);
}

异常捕获类

抛出异常就需要有地方去捕获、定义异常捕获类:ExceptionCatch

使用@ControllerAdvice。

使用@ExceptionHandler捕获CustomException.class这个类的类型的异常。然后就可以获取到这个异常,并返回ReponseResult

@ControllerAdvice
public class ExceptionCatch {
//捕获CustomException此类异常
@ExceptionHandler(CustomException.class)
public ResponseResult customException(CustomException customException){
ResultCode resultCode=customException.getResultCode();
return new ResponseResult(resultCode);
}
}

增加日志

注意Logger这个类是org.slf4j这个命名空间下的

完整代码

@ControllerAdvice
public class ExceptionCatch {
private static final Logger LOGGER= LoggerFactory.getLogger(ExceptionCatch.class);
//捕获CustomException此类异常
@ExceptionHandler(CustomException.class)
public ResponseResult customException(CustomException customException){
//获取异常信息,日志记录异常
LOGGER.error("catch exception:{}",customException.getMessage());
ResultCode resultCode=customException.getResultCode();
return new ResponseResult(resultCode);
}
}
public class CustomException extends RuntimeException {
ResultCode resultCode; public CustomException(ResultCode resultCode){
this.resultCode=resultCode;
} public ResultCode getResultCode() {
return resultCode;
}
}

最新文章

  1. App Extension访问Cocoapods引入的第三方库
  2. Android Activity中获取当前焦点的控件,自动化输入EditText
  3. Linux下统计高速网络中的流量
  4. BASE64,MD5,SHA,HMAC加密與解密算法(java)
  5. PHP 表单 - 验证邮件和URL
  6. 开源入侵检测系统OSSEC搭建之一:服务端安装
  7. Android实例-操作sqlite数据之自建导航(XE8+小米2)
  8. c语言知识(找出大于2门成绩不及格的学生)
  9. java基础(八) 面向对象(三)
  10. LayoutInflater作用及使用--自定义EditText,自带清除内容按钮
  11. huffman编码——原理与实现
  12. 2015 多校联赛 ——HDU5350(huffman)
  13. Nginx 过滤sub模块
  14. jupyter notebook添加虚拟环境
  15. Lua代码规范
  16. linux同步Internet时间
  17. 1.SpringMVC入门
  18. jdk1.8.0_45源码解读——Map接口和AbstractMap抽象类的实现
  19. PhoneGap+Cordova+SenchaTouch-01-环境搭建
  20. Android 使用ListView显示信息列表

热门文章

  1. Django drf:视图层封装、ViewSetMixin、路由配置、解析器、响应器
  2. java实现网络请求超时自动熔断
  3. ndk学习之c++语言基础复习----面向对象编程
  4. Java 基础 - 泛型类/泛型方法/类型通配符'?' 的用法及栗子
  5. Java多维数组定义以及常见异常
  6. waitpid()
  7. 5.7 zip 版本的安装 以及遇到的坑
  8. mysql中对表操作----为所有列插入数据
  9. Oracle自动化安装脚本-part02-亲试ok
  10. 2019牛客多校第四场A meeting——树的直径