为什么我们不能在Java 8流中抛出异常? [英] Why can't we throw exception inside Java 8 stream?

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

问题描述

例如:

Person result = persons.stream()
                  .filter(x -> {
                    if ("test".equals(x.getName() ) ) {
                      throw new IOException("not possible inside stream y ?"); //any checked exception
                    }
                    //code here
                  })

我正在寻找不允许使用该产品的原因?即使声明代码的方法抛出IOException

M looking for reason why it is not being allowed ? Even if method in which code is declared throws IOException

推荐答案

您的代码运行正常,除了throw new ...行末尾缺少半角冒号,而且缺少可能被隐藏的return语句在// code here中.

your code is working perfectly, apart from a missing semi colon at the end of the throw new ... line, and from a missing a return statement that may be hidden in // code here.

您不能做的是抛出一个已检查的异常(不是RuntimeException),因为已检查的异常是方法签名的一部分,而Predicate.test方法没有声明一个.

What you can't do is throw a checked exception (which RuntimeException is not) because checked exception are part of the method signature and the Predicate.test method do not declare one.

为了更精确地了解正在发生的事情以及为什么不能在此处抛出检查后的异常,这是在没有lambda的情况下如何编写代码的方法:

EDIT : To see more precisely what is happening and why you can't throw a checked exception here, here is how you could have written the code without lambda :

从这里开始:

public Person myMethod() throws IOException {
    Person result = persons.stream()
              .filter(x -> {
                if ("test".equals(x.getName() ) ) {
                  throw new IOException("not possible inside stream y ?"); //any checked exception
                }
                //code here
                return true;
              });
    return person;
}

对此:

public Person myMethod() throws IOException {
    Person result = persons.stream()
              .filter(new Predicate<Person>() {
                  public boolean test(Person x) {
                      if ("test".equals(x.getName() ) ) {
                          throw new IOException("not possible inside stream y ?"); //any checked exception
                      }
                      //code here
                      return true;
                  }
              });
    return person;
}

如您所见,lambda表达式中的代码现在位于匿名Predicate类的test方法中,该类未声明任何已检查的异常.

As you can see, the code inside the lambda expression is now inside the test method of an anonymous Predicate class, which is not declaring any checked exception.

为什么Predicate?因为这是filter方法所期望的,并且您可以使用lambda代替常规对象,因为它是单个方法接口:只有test是抽象的,并且lambda签名应与方法.

Why Predicate ? Because it is what the filter method is expecting, and you can use a lambda instead of a conventional object because it is a single method interface : only test is abstract, and your lambda signature should be the same as the Predicate.test method.

如果您真的希望能够处理检查的异常,请

If you really wish to be able to handle checked exceptions, the linked post (in a comment from Frederico) show some ways to bypass these limitations.

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

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