Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we throw exception inside Java 8 stream?

E.g:

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

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

like image 429
Om. Avatar asked Dec 24 '22 20:12

Om.


2 Answers

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.

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.

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 :

From this :

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;
}

to this :

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;
}

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.

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.

like image 185
Thierry Avatar answered Jan 03 '23 10:01

Thierry


looking for reason why it is not being allowed ?

You are implementing a Predicate which doesn't declare a checked Exception.

Exceptions are side effects and no favoured by functional designers and most likely this is the reason they are not supported very well.

I use a trick to pass the checked exception out and catch it later. Another option is to wrap it with an unchecked exception or use a class like IORuntimeException In different cases I use all three options.

https://vanilla-java.github.io/2016/06/21/Reviewing-Exception-Handling.html

like image 33
Peter Lawrey Avatar answered Jan 03 '23 12:01

Peter Lawrey