How to handle Exception in Java 8 Stream?
20,994
Solution 1
You probably have too many responsibilities in your method. You should think about splitting it into a method that only maps and another one that gathers them.
private List<Result> getResultList(List<String> names) throws ResultClassException {
try {
return names.stream()
.map(this::getOrCreateResult)
.collect(collectingAndThen(toList(), Collections::unmodifiableList));
} catch (RuntimeException e) {
if (e.getCause() instanceof CustomException) {
throw new ResultClassException("Error", e.getCause());
}
throw e;
// Or use Guava's propagate
}
}
private Result getOrCreateResult(String name) {
if (!resultRepository.contains(name)) {
try {
return createResult(name);
} catch (CustomException e) {
throw new RuntimeException(e);
}
} else {
log.info("Result {} already exists.", name);
return resultRepository.get(name);
}
}
Solution 2
I wouldn't suggest using RuntimeException as that would drive you into poor coding practice. Try to handle ResultClassException in the calling method of getResultList(...).
Author by
user3407267
Updated on July 09, 2022Comments
-
user3407267 almost 2 years
I have a method where I am traversing through a List and creating List. While doing so, I am calling a method(createResult) to will give a Result also throws CustomException which I am wrapping as ResultClassException. But I keep getting an error saying Unhandled Exception.
My Code :
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); }
Can Someone tell what I am doing wrong?