I have a following method.
private InternetAddress[] constructToRecipients(final List<String> to) {
if (to.isEmpty()) {
throw new IllegalArgumentException("To list can not be empty");
}
return to.stream().map(r -> parseEmailAddress(r)).collect(Collectors.toList()).toArray(new InternetAddress[0]);
}
private InternetAddress parseEmailAddress(final String address) {
try {
return InternetAddress.parse(address)[0];
}
catch (final AddressException e) {
throw new IllegalArgumentException("Invalid email", e);
}
}
I would like to make the parseEmailAddress method throws AddressException like,
private InternetAddress parseEmailAddress(final String address) throws AddressException {
return InternetAddress.parse(address)[0];
}
But, not sure how to handling this expception in the caller from lambda.
And, the below call should handle the exception gracefully and throw exception further if the final list is empty.
return to.stream().map(r -> parseEmailAddress(r)).collect(Collectors.toList()).toArray(new InternetAddress[0]);