为什么我不能在 Java 8 lambda 表达式中抛出异常? [英] Why can't I throw an exception in a Java 8 lambda expression?

查看:52
本文介绍了为什么我不能在 Java 8 lambda 表达式中抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我升级到 Java 8 并尝试用新的 lamdba 表达式替换通过 Map 的简单迭代.循环搜索空值,如果找到则抛出异常.旧的 Java 7 代码如下所示:

I upgraded to Java 8 and tried to replace a simple iteration through a Map with a new lamdba expression. The loop searches for null values and throws an exception if one is found. The old Java 7 code looks like this:

for (Map.Entry<String, String> entry : myMap.entrySet()) {
    if(entry.getValue() == null) {
        throw new MyException("Key '" + entry.getKey() + "' not found!");
    }
}

我尝试将其转换为 Java 8 如下所示:

And my attempt to convert this to Java 8 looks like this:

myMap.forEach((k,v) -> {
    if(v == null) {
        // OK
        System.out.println("Key '" + k+ "' not found!");

        // NOK! Unhandled exception type!
        throw new MyException("Key '" + k + "' not found!");
    }
});

谁能解释为什么这里不允许使用 throw 语句以及如何更正?

Can anyone explain why the throw statement not allowed here and how this could be corrected?

Eclipse 的快速修复建议在我看来并不正确……它只是用 try-catch 块包围了 throw 语句:

Eclipse's quick-fix suggestion does not look right to me... it simply surrounds the throw statement with a try-catch block:

myMap.forEach((k,v) -> {
    if(v == null) {
        try {
            throw new MyException("Key '" + k + "' not found!");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
});

推荐答案

不允许抛出已检查的异常,因为 accept(T t, U u) java.util.function.BiConsumer 接口没有在其 throws 子句中声明任何异常.而且,如您所知,Map#forEach 采用这种类型.

You are not allowed to throw checked exceptions because the accept(T t, U u) method in the java.util.function.BiConsumer<T, U> interface doesn't declare any exceptions in its throws clause. And, as you know, Map#forEach takes such a type.

public interface Map<K, V> {

    default void forEach(BiConsumer<? super K, ? super V> action) { ... }

}                        |
                         |
                         V
@FunctionalInterface
public interface BiConsumer<T, U> {

    void accept(T t, U u); // <-- does throw nothing

}

当我们谈论检查异常时确实如此.但是您仍然可以抛出未经检查的异常(例如 java.lang.IllegalArgumentException):

That is true when we are talking about checked exceptions. But you still can throw an unchecked exception (e.g. a java.lang.IllegalArgumentException):

new HashMap<String, String>()
    .forEach((a, b) -> { throw new IllegalArgumentException(); });

这篇关于为什么我不能在 Java 8 lambda 表达式中抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆