Java语法中Lambda表达式无法抛出异常

1.Demo 例子: 错误提示 - Unhandled exception: java.io.IOException;

public static void main(String[] args) throws IOException{
   Stream.of("a", "b", "c").forEach(str -> {
        throw new IOException();
   });
}

2.编译通过 
Stream.of("a", "b", "c").forEach(str -> {
    throw new RuntimeException(new IOException());
});  // 不建议使用

或者 

static void doThrow(Exception e) throws E {
    throw (E)e;
}

//编译通过
Stream.of("a", "b", "c").forEach(str -> {
    doThrow(new IOException());
});

 

参考  - https://www.jianshu.com/p/a3855f880ff2

你可能感兴趣的:(Java语法中Lambda表达式无法抛出异常)