简体   繁体   English

为什么不能在方法级别抛出Java流中的检查异常?

[英]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 ,或者通过创建处理errorwrapper 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.

相关问题 为什么在以下Java Stream过滤器方法中未引发NullPointerException - Why NullPointerException is not thrown in the following Java Stream filter method 如果系统抛出已检查的异常,是否可以被默认处理程序捕获? - can checked exceptions, if thrown by system, be caught by the default handler? 我在Java中抛出的异常无法正常工作 - My thrown exceptions in Java aren't working java:包装已检查异常的标准方法 - java: standard method of wrapping checked exceptions 为什么Java中没有实例级的Stream.concat方法? - Why is there no Instance-level Stream.concat method in Java? 为什么java编译器不检查未经检查的异常? - why are unchecked exceptions not checked by java compiler? 为什么一个方法可以被声明为抛出许多异常,即使它们都没有被抛出? - Why can a method be declared to throw many exceptions even if none of them are thrown? 为什么Java 8类型推断不考虑Lambdas在重载选择中引发的异常? - Why Doesn't Java 8 Type Inference Consider Exceptions Thrown by Lambdas in Overload Selection? 为什么在正常方法调用不需要的情况下,java.lang.reflect.Proxy需要声明检查的异常? - Why does a java.lang.reflect.Proxy require declaring checked exceptions when normal method invocation does not? 对于特定方法,如何使用反射查看java中的已检查异常? - For a particular method, how can I view the checked exceptions in java using reflection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM