Is there any support for handling custom exceptions inside Collectors.toMap. I call a method inside Collector.toMap that throws MyException. Can it be restored in the pupulateValues () calling function ? To demonstrate, I used the code below to recover MyException, but could not get through. My goal is to handle MyException in the main method.
public static void main(String[] args){
try {
pupulateValues();
} catch (MyException e) {
e.printStackTrace();
}
}
private static void pupulateValues() throws MyException{
Map<String,String> map = new HashMap<>();
map.put("asdf", "asdf");
map.put("ss", "fff");
map.put("aaaaaa", "aaaaaaa");
Map<String,String> map2=map.entrySet().stream().collect(
Collectors.toMap(entry->entry.getKey(),entry-> {
try {
return getCert(entry.getValue());
} catch (MyException e) {
throw new MyException();
}}));
}
static String getCert(String val) throws MyException {
if(val == null) {
throw new MyException("Some exception");
}
return val;
}
source
share