波神

导航

try-catch+thows异常范围说明

方式一:

        CatalogPO deleteTarget = null;
        /** 查询是否存在 **/
        deleteTarget = catalogMapper.findByCatalogId(catalogId);
        if (Objects.isNull(deleteTarget)) {
            log.warn("the delete catalog is not existed");
            throw new WhBuCatalogException(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.SUFIX_DB_DELETE, "要删除的服务目录不存在");
        }
        /** 判断是不是是预置服务目录 **/
        if (deleteTarget.getPreseted() == 1) {
            log.warn("the preseted catalog is not allow delete");
            throw new WhBuCatalogException(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.SUFIX_DB_DELETE, "预置服务目录不允许删除");
        }
        /** 判断服务目录是否有绑定的服务 **/
        Metadata metadata = new Metadata();
        metadata.setFilter(new TreeMap<>());
        metadata.getFilter().put("catalogId", catalogId.toString());
        metadata.setPaginationParam(new PaginationParam(1,1000,1000));
        List<WhBuService> services = catalogMapper.listBindedServices(metadata);
        if (CollectionUtils.isNotEmpty(services)) {
            log.warn("the catalog has bind service is not allow delete");
            throw new WhBuCatalogException(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.SUFIX_DB_DELETE, "服务目录绑定有服务不允许删除");
        }
        try {
            List<Long> ids = findCatalogIdAndChildIds(catalogId);
            catalogMapper.deleteCatalogByIds(ids);
            return mapper.map(deleteTarget,CatalogDO.class);
        } catch (Exception e) {
            log.error("delete catalog error,{}", e);
            throw new WhBuCatalogException(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.SUFIX_DB_DELETE, "删除服务目录方法失败");
        }

 红色地方抛出异常,则紫色的地方不会继续执行

 

方式二:

        CatalogPO deleteTarget = null;
        try {
            /** 查询是否存在 **/
            deleteTarget = catalogMapper.findByCatalogId(catalogId);
            if (Objects.isNull(deleteTarget)) {
                log.warn("the delete catalog is not existed");
                throw new WhBuCatalogException(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.SUFIX_DB_DELETE, "要删除的服务目录不存在");
            }
            /** 判断是不是是预置服务目录 **/
            if (deleteTarget.getPreseted() == 1) {
                log.warn("the preseted catalog is not allow delete");
                throw new WhBuCatalogException(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.SUFIX_DB_DELETE, "预置服务目录不允许删除");
            }
            /** 判断服务目录是否有绑定的服务 **/
            Metadata metadata = new Metadata();
            metadata.setFilter(new TreeMap<>());
            metadata.getFilter().put("catalogId", catalogId.toString());
            metadata.setPaginationParam(new PaginationParam(1,1000,1000));
            List<WhBuService> services = catalogMapper.listBindedServices(metadata);
            if (CollectionUtils.isNotEmpty(services)) {
                log.warn("the catalog has bind service is not allow delete");
                throw new WhBuCatalogException(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.SUFIX_DB_DELETE, "服务目录绑定有服务不允许删除");
            }
        } catch (Exception e) {
            log.error("delete catalog error,{}", e);
        }
        try {
            List<Long> ids = findCatalogIdAndChildIds(catalogId);
            catalogMapper.deleteCatalogByIds(ids);
            return mapper.map(deleteTarget,CatalogDO.class);
        } catch (Exception e) {
            log.error("delete catalog error,{}", e);
            throw new WhBuCatalogException(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.SUFIX_DB_DELETE, "删除服务目录方法失败");
        }

红色地方抛出异常,则紫色的地方还会继续执行

 

posted on 2018-05-18 17:31  波神  阅读(808)  评论(0编辑  收藏  举报