我在尝试Java 8的Lambda表达式时遇到了问题。通常它工作正常,但现在我有方法可以抛出IOException。最好看一下以下代码:class Bank{ .... public Set<String> getActiveAccountNumbers() throws IOException { Stream<Account> s = accounts.values().stream(); s = s.filter(a -> a.isActive()); Stream<String> ss = s.map(a -> a.getNumber()); return ss.collect(Collectors.toSet()); } ....}interface Account{ .... boolean isActive() throws IOException; String getNumber() throws IOException; ....}问题是,它不能编译,因为我必须捕获isActive-和getNumber-Methods的可能例外。但即使我明确使用如下所示的try-catch-Block,它仍然无法编译,因为我没有捕获异常。所以要么JDK中存在错误,要么我不知道如何捕获这些异常。class Bank{ .... //Doesn't compile either public Set<String> getActiveAccountNumbers() throws IOException { try{ Stream<Account> s = accounts.values().stream(); s = s.filter(a -> a.isActive()); Stream<String> ss = s.map(a -> a.getNumber()); return ss.collect(Collectors.toSet()); }catch(IOException ex){ } } ....}我怎样才能让它发挥作用?有人能暗示我找到正确的解决方案吗?
添加回答
举报
0/150
提交
取消