나는 목록을 탐색하고 목록을 만드는 방법이 있습니다. 그렇게하는 동안 Result (ResultRassult)를 호출하여 ResultClassException으로 래핑하는 CustomException을 throw합니다. 하지만 처리되지 않은 예외를 말하는 오류가 계속 발생합니다.Java 8 Stream에서 Exception을 처리하는 방법은 무엇입니까?
내 코드 :
private List<Result> getResultList(List<String> results) throws ResultClassException {
List<Result> resultList = new ArrayList<>();
results.forEach(
(resultName) -> {
if (!resultRepository.contains(resultName)) {
try {
final Result result = createResult(resultName);
resultList.add(result);
} catch (CustomException e) {
throw new ResultClassException("Error",e);
}
} else {
resultList.add(resultRepository.get(resultName));
log.info("Result {} already exists.", resultName);
}
}
);
return Collections.unmodifiableList(resultList);
}
은 누군가가 내가 잘못 뭐하는 거지 말할 수 있습니까?
'ResultClassException'이 (가) RuntimeException 또는 ResultRepository.get (resultName)의 하위 클래스가 아니며 체크 예외를 throw합니다. 이 람다는'Consumer' 함수 인터페이스에 해당하지 않기 때문에'forEach'의 람다에서 검사 된 예외를 던질 수 없습니다. –
코드에 스트림이 표시되지 않고 단지 forEach 만 표시됩니다. 왜 보통의 for 루프를 사용하지 않겠습니까? – shmosel