有没有办法在使用Java 8中的消费者时抛出异常?
例如:
例如:
private void fooMethod(List<String> list) throws Exception {
list.forEach(element->{
if(element.equals("a")) {
throw new Exception("error!");
}
});
}
这个报错提示: Unhandled exception type Exception
在这种情况下,正确的抛出异常方式是什么?
if(list.contains("a")) throw new Exception("error!");
- Holger