Existe-t-il un support pour la gestion des exceptions personnalisées dans Collectors.toMap? J'appelle une méthode dans Collector.toMap qui lance MyException. Peut-il être renvoyé dans la fonction d'appel pupulateValues ()? Pour la démonstration, j'ai utilisé le code ci-dessous pour réécrire MyException mais je n'ai pas pu le faire. Mon objectif est de gérer MyException dans la méthode principale.java 8 Gestion des exceptions personnalisées lors de l'utilisation de Collectors.toMap
public static void main(String[] args){
try {
pupulateValues();
} catch (MyException e) {
// do something
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) {
// TODO Auto-generated catch block
throw new MyException();
}}));
}
static String getCert(String val) throws MyException {
if(val == null) {
throw new MyException("Some exception");
}
return val;
}
4. Utiliser une boucle de flux –