【发布时间】:2019-04-15 13:12:25
【问题描述】:
实际上我想抛出两个异常作为检查两个条件的一部分。但是想知道如何在流式传输和映射后抛出这些异常。
这是使用 Streams 转换为 Java-8 的代码
for(GroupCallCenter existingGroupCall : group.getGroupCallCenters())
{
if(!existingGroupCall.getCallCenter()
.getId().equals(accountCallCenterResource.getCallCenterId()))
{
if(!accountCallCenterResource.getValidity().getEffectiveStarting().isAfter(existingGroupCall.getExpirationDate())&&!existingGroupCall.getEffectiveDate().isAfter(accountCallCenterResource.getValidity().getExpiresAfter()))
{
throw new ApiException(ApiErrorCode.DEFAULT_400, "Group call
center already exist for that period");
}
}
else {
throw new DuplicateException(ApiErrorCode.DUPLICATE,
existingGroupCall.getId());
}
}
【问题讨论】:
-
一次只能抛出一个异常。但是,您可以构建一个包含错误的列表并将其返回,或者将其添加到您抛出的异常中
-
您的问题不清楚。您是在询问将循环转换为流操作吗?
-
是的,我正在尝试将循环转换为流并在那里抛出异常
-
如果没有随意的缩进,您的代码很难阅读。也许你想修复它。
-
转换为java-stream的原因是什么?