简体   繁体   中英

Why can't checked exceptions in a java stream be thrown at method level?

I've been learning about concurrency and the streams API and came across this. The offerLast() method can throw InterruptedException , so I get that I must handle it. What I don't get is why can't I throw it at the method level by adding throws Exception ?. As it is this code does not compile.

static BlockingDeque<Integer> queue = new LinkedBlockingDeque<>();

public static void testing() throws Exception {
    IntStream.iterate(1, i -> i+1).limit(5)
            .parallel()
            .forEach(s -> queue.offerLast(s, 10000, TimeUnit.MILLISECONDS));
}

I know it can be solved by surrounding it in a try/catch , or by creating a wrapper method that handles the error , but I'm still trying to understand why it can't be thrown at the method level.

Because lambda expressions are not always evaluated immediately.

Let's you have this:

public Supplier<String> giveMeASupplier() throws Exception {
    return () -> someMethodThatThrowsCheckedException()
}

According to you, the above would work. Right?

Now in another method, I can do this:

Suppler<String> supplier = null;
try {
    supplier = giveMeASupplier() // no exception is thrown here.
} catch (Exception ex) {
    ex.printStackTrace();
}
if (supplier != null) {
    System.out.println(supplier.get()); // this might throw an exception! Yet it's not in a try...catch!
}

Now what do you think would happen if supplier.get() throws an exception? Is there anything to catch it? No. If somehow the catch block a few lines before gets run, then it would be really weird.

The simple answer is that the "method" you're referring to is Consumer.accept , not YourClass.testing .

The lambda s -> queue.offerLast(s, 10000, TimeUnit.MILLISECONDS) is an implementation of java.util.function.Consumer.accept(T) , which doesn't declare that it can throw InterruptedException .
And this behavior is not particular to streams, wherever a lambda expression is defined, it must comply with the signature of the abstract method of the functional interface it implements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM