简体   繁体   English

错误:未报告的异常IOException; 必须被捕获或声明被抛出流()

[英]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的签名需要ConsumerConsumer不声明其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: 推荐的解决方案是:

  • catch and handle it within the lambda, or 在lambda中捕获并处理它,或者
  • catch it in the lambda and rethrow it wrapped in an unchecked exception, then catch / handle the wrapped exception at the outer level. 在lambda中捕获它并在未经检查的异常中重新抛出它,然后在外层捕获/处理包装的异常。 @Hank D's answer illustrates this. @Hank D的回答说明了这一点。

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.

相关问题 未报告的异常IOException,必须捕获或声明为抛出 - Unreported exception IOException, must be caught or declared to be thrown Java错误:必须捕获未声明的异常ioexception或将其声明为引发 - Java error: unreported exception ioexception must be caught or declared to be thrown Java错误未报告的异常IOException; 必须被抓住或宣布被抛出 - Java Error unreported exception IOException; must be caught or declared to be thrown Java类中的错误“必须捕获或声明抛出未报告的异常java.io.ioexception” - Error “unreported exception java.io.ioexception must be caught or declared to be thrown” in Java class 错误消息“未报告的异常 java.io.IOException;必须被捕获或声明为抛出” - Error message "unreported exception java.io.IOException; must be caught or declared to be thrown" 错误:未报告的异常java.io.IOException; 必须被抓住或宣布被抛出 - ERROR: unreported exception java.io.IOException; must be caught or declared to be thrown 未报告的例外例外; 必须被抓住或宣布被抛出 - unreported exception Exception; must be caught or declared to be thrown 错误未报告的异常ClassNotFoundException; 必须被抓住或宣布被抛出 - error unreported exception ClassNotFoundException; must be caught or declared to be thrown 编译错误:未报告的异常添加; 必须被捕获或声明被抛出 - Compile error: unreported exception Add; must be caught or declared to be thrown 错误:未报告的异常InterruptedException; 必须被捕获或声明为抛出(调用AsyncTask) - error: unreported exception InterruptedException; must be caught or declared to be thrown (calling AsyncTask)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM