[英]error: unreported exception IOException; must be caught or declared to be thrown in stream()
I'm trying to write a File and here is the code: 我正在尝试编写一个文件,这里是代码:
public static void writeFile(File f, int n) throws IOException{
//reads n teams which go to next phase
try(BufferedWriter writer = new BufferedWriter(new FileWriter(f))){
table.stream()
.sorted()
.limit(n)
.forEachOrdered(x->writer.write(x.getName()));
/*for(int i=0;i<n;i++){
writer.write(table.get(i).getName());
writer.newLine();
}*/ this if uncommented works great!
}catch (IOException e){
e.printStackTrace();
}
}
and I get this error when I try to compile: 我尝试编译时收到此错误:
Counter.java:123: error: unreported exception IOException; Counter.java:123:错误:未报告的异常IOException; must be caught or declared to be thrown .forEachOrdered(x->writer.write(x.getName()));
必须被捕获或声明被抛出.forEachOrdered(x-> writer.write(x.getName()));
My doubt: Even if I declare the IOException in the method I am using the .forEachOrdered() I can't use it because the method which I define inside the .forEachOrdered() (which is a Consumer) doesn't declare it? 我的疑问:即使我在方法中声明IOException我使用.forEachOrdered()我也不能使用它,因为我在.forEachOrdered()(它是Consumer)中定义的方法没有声明它? I don't know if I am clear, but maybe you could infer something just looking at the code.
我不知道我是否清楚,但也许你可以推断一下只看代码。 Thanks.
谢谢。
Note: the table is a declared static list with Teams. 注意:该表是带有团队的声明静态列表。
You could catch the IOException
and rethrow it wrapped in an UncheckedIOException, like this: 你可以捕获
IOException
并重新抛出它包含在UncheckedIOException中,如下所示:
.forEachOrdered(x-> {
try{
writer.write(x.getName()));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
The stream will not continue processing if an IOException is thrown. 如果抛出IOException,则流将不会继续处理。
Outside of the stream, you should then change your catch to 在流之外,您应该将捕获更改为
} catch (IOException | UncheckedIOException e) {
e.printStackTrace();
}
You might create convenience functional types that take care of the try/catch for you, something like 您可以创建便利的函数类型来处理try / catch,例如
@FunctionalInterface
public interface CheckedIOConsumer<T> {
void accept(T t) throws IOException;
static <T> Consumer<T> toUnchecked(CheckedIOConsumer<T> consumer) {
return t -> {
try {
consumer.accept(t);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}
}
which would make your terminal function 这将使您的终端功能
.forEachOrdered(toUnchecked(x -> writer.write(x.getName())));
The problem is that the signature for forEachOrdered
requires a Consumer
and a Consumer
does not declare that its accept
method can throw any checked exceptions. 问题是
forEachOrdered
的签名需要Consumer
而Consumer
不声明其accept
方法可以抛出任何已检查的异常。
JLS 11.2.3 says: JLS 11.2.3说:
It is a compile-time error if a lambda body can throw some exception class E when E is a checked exception class and E is not a subclass of some class declared in the throws clause of the function type targeted by the lambda expression.
当一个lambda体可以抛出一些异常类E时,如果E是一个经过检查的异常类,并且E不是一个在lambda表达式所针对的函数类型的throws子句中声明的某个类的子类,那么这是一个编译时错误。
Therefore, the IOException
must be caught by the lambda itself. 因此,必须由lambda本身捕获
IOException
。 The recommended solutions are: 推荐的解决方案是:
The problem with handling the IOException within the lambda is that the "for each" will continue running. 在lambda中处理IOException的问题是“for each”将继续运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.