• 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_16-异常处理-可预知异常处理-自定义异常类型和抛出类


    在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;
    }
    }
  • 相关阅读:
    try
    mysql 遇到的问题
    java POI(二)
    Spring/SpringBoot整合QuartZ
    Spring整合QuartZ
    Idea使用指南--实用版
    QuartZ
    Spring Task
    Spring01-模块划分
    国际化、文件上传下载
  • 原文地址:https://www.cnblogs.com/wangjunwei/p/11576666.html
Copyright © 2020-2023  润新知