Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handle exceptions in stream java 8

I have a string:

"1, 2, 3 , -4"

it is split by ", ". I have a function to convert a number to a enum type which works fine. I want to use java 8 to convert this string to a list of enum objects.

Pattern pattern = Pattern.compile(", ");
List<f> fList = pattern.splitAsStream(str)
  .map(s -> {
    try {
      return this.getEnumObject(Integer.valueOf(s), f.class);
    }
    catch (NoEleException e) {
      e.printStackTrace();
    }
  })
  .collect(Collectors.toList());

This gives me an error:

missing return type.

How could I fix it?

like image 212
user3369592 Avatar asked Nov 15 '17 23:11

user3369592


People also ask

Can you handle exception in stream?

From a stream processing, we can throw a RuntimeException. It is meant to be used if there is a real problem, the stream processing is stopped; Or if we don't want to stop the whole processing, we only need to throw a caught Exception. Then it has to be handled within the stream.

Does Java 8 support streams?

Java 8 offers the possibility to create streams out of three primitive types: int, long and double. As Stream<T> is a generic interface, and there is no way to use primitives as a type parameter with generics, three new special interfaces were created: IntStream, LongStream, DoubleStream.

Can we throw checked exception in Java stream?

If a method which is used within a Java 8 stream processing throws a checked exception, this exception has to be handled. One way to do this is to wrap the checked exception with a java. lang. RuntimeException and throw it.


2 Answers

Currently, if an exception occurs no result will be returned hence the compilation error. You'll need to return a value after the catch block .

like image 112
Ousmane D. Avatar answered Oct 13 '22 02:10

Ousmane D.


Basically to ways of managing this:

  1. catching the exception and return some value or encapsulate values in Optionals and filter accordingly
  2. Throwing a RuntimeException which chains the original one

In the first case we use Optional to put something into the stream on error, and then manage these empty values further in the stream:

pattern.splitAsStream(str)
.map(s -> {
  try {
    return Optional.of(this.getEnumObject(Integer.valueOf(s), f.class));
  }
  catch (NoEleException e) {
    e.printStackTrace();
    return Optional.empty();
  }
 })
.filter(Optional::isPresent) // remove empty optionals
.map(Optional::get) // unwrap them
.collect(Collectors.toList());

In the second case the stream is stopped and you can then try to catch the RuntimeException and unchain the original one:

pattern.splitAsStream(str)
.map(s -> {
  try {
    return Optional.of(this.getEnumObject(Integer.valueOf(s), f.class));
  }
  catch (NoEleException e) {
    e.printStackTrace();
    throw new RuntimeException(e); // stop the stream
  }
 })
.collect(Collectors.toList());
like image 45
Jean-Baptiste Yunès Avatar answered Oct 13 '22 02:10

Jean-Baptiste Yunès