I was following the article https://www.oreilly.com/ideas/handling-checked-exceptions-in-java-streams for extracting a method into a method, in order to handle exceptions, and noticed the examples given only look clean since they don't actually compile because they miss a return statement.
Essentially I'm looking to have a parse method similar to this "divide" in the article example 3, but mine parses the list and in some scenarios it will throw errors log them and continue, like this:
public List<String> validator(List<String> values) {
return values.stream()
.map(this::parse)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
public String parse(String s){
try{
// returns something or throws exceptions
} catch (Exception e ){
log(e)
}
return null;
}
As you can see from this code I cheated the compilation error by returning a null at the end of the parse method that I then filter nulls on my validator before collecting, and it looks appalling. Is there a better way to skip the values in a clean easy way?