handle exceptions in stream java 8

user3369592 :

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?

Ousmane D. :

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 .

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related