[英]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. 我一直在学习有关并发性和流
API
。 The offerLast()
method can throw InterruptedException
, so I get that I must handle it. offerLast()
方法可以引发InterruptedException
,因此我必须处理它。 What I don't get is why can't I throw it at the method level by adding throws Exception
?. 我不明白的是为什么我不能通过添加
throws Exception
将它扔到方法级别? As it is this code
does not compile. 因为它是此
code
无法编译。
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. 我知道可以通过将其包围在
try/catch
,或者通过创建处理error
的wrapper method
来解决它,但是我仍在尝试理解为什么不能在方法级别将其抛出。
Because lambda expressions are not always evaluated immediately. 因为lambda表达式并不总是立即求值。
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? 现在,如果
supplier.get()
抛出异常,您会怎么办? Is there anything to catch it? 有什么要抓住的吗? No. If somehow the
catch
block a few lines before gets run, then it would be really weird. 否。如果
catch
在运行前以某种方式阻塞了几行,那将是很奇怪的。
The simple answer is that the "method" you're referring to is Consumer.accept
, not YourClass.testing
. 简单的答案是,您所指的“方法”是
Consumer.accept
,而不是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
. lambda
s -> queue.offerLast(s, 10000, TimeUnit.MILLISECONDS)
是java.util.function.Consumer.accept(T)
,该声明没有声明可以抛出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. 而且,这种行为并不特定于流,无论在哪里定义了lambda表达式,它都必须遵守其实现的功能接口的抽象方法的签名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.