我已经看到了一些与此相关的问题,但我需要一个明确的答案。我的确了解了lamba表达式运行的环境以及副作用的概念,但我相信我没有在这里看到一种解决方法。Java 8流。抛出lamba表达式中客户端方法的异常
我需要根据他们的性别来映射角色列表,但是我用来确定他们性别的方法会返回一个检查过的异常,而Collectors.groupingBy根本就不喜欢这个异常。
摆脱检查的异常不是一个选项,我需要将它发送给调用我的代码片段的客户端方法。我有什么可以做的吗?
public class Example {
public static void main(String[] args) {
Example example = new Example();
try {
example.runExample();
} catch (MException e) {
//Place where I want to handle the exception
}
}
private void runExample() throws MException{
List<Person> personas = Arrays.asList(new Person("Sergio", "234456789", 35), new Person("Mariana", "123456789", 38));
Map<String, List<Person>> personsBySex = personas.stream().collect(Collectors.groupingBy(persona -> {
try {
return getSex(persona.getSSN());
} catch (MException e) {
}
return null;
//Compiler forces me to return a value, but I don't want to return null.
//I want to throw up the MException to the client method (main)
}));
}
private String getSex(String ssn) throws MException {
// Imagine here is a call to an outside service that would give me the
// sex based on the SSN, but this service could return an exception as
// well
if (ssn.isEmpty())
throw new MException();
return ssn.startsWith("1") ? "Female" : "Male";
}
}
class Person {
private String name, ssn;
private Integer age;
public Person(String name, String ssn, Integer age) {
this.name = name;
this.ssn = ssn;
this.age = age;
}
public String getName() {return name;}
public String getSSN() {return ssn;}
public Integer getAge() {return age;}
}
class MException extends Exception {
}
感谢您的任何想法!
我发现[这个问题](https://stackoverflow.com/questions/27644361/how-can-i-throw-checked-exceptions-from-inside-java -8流),其评论和回答非常有趣。 Tl; dr:你必须处理这个例外情况,但正如下面的答案和链接问题所显示的那样,你可以用某些模式或utils来解决它,甚至可以隐藏这些模式。 –