可以在selectByUnique()中抛出异常吗?
来源:9-20 为车次&车厢&车站增加存在性校验

慕神4535282
2023-05-10
老师,下午好!
请教一个问题,在代码
private Station selectByUnique(String name) {
StationExample stationExample = new StationExample();
stationExample.createCriteria().andNameEqualTo(name);
List<Station> list = stationMapper.selectByExample(stationExample);
if (CollUtil.isNotEmpty(list)) {
return list.get(0);
} else {
return null; // 这一句写成 throw new BusinessException(BusinessExceptionEnum.BUSINESS_STATION_NAME_UNIQUE_ERROR);
}
}
不好吗?这样代码关联不是更密切吗?
写回答
1回答
-
查数据的方法就只负责查数据,不负责处理逻辑,throw 相当于中断线程了,要不要中断是由外层方法来判断,举个例子,比如用户注册,要首先查手机是否已存在,不存在就去注册,不存在是合理的,不能throw
10
相似问题