1

Not able to wrap stream object in a try/catch block.

I tried like this:

reponseNodes.stream().parallel().collect(Collectors.toMap(responseNode -> responseNode.getLabel(), responseNode -> processImage(responseNode)));

Eclipse started complaining underlining processImage(responseNode) and suggested that it needs to Surround with try/catch.

Then I updated to:

return reponseNodes.stream().parallel().collect(Collectors.toMap(responseNode -> responseNode.getLabel(), responseNode -> try { processImage(responseNode) } catch (Exception e) { throw new UncheckedException(e); }));

Updated code also did not work.

2
  • Could you type out the exact warning/error Eclipse is giving you? Commented Dec 6, 2015 at 6:13
  • Eclipse warning Surround with try/catch underlining processImage(responseNode). Commented Dec 6, 2015 at 6:18

1 Answer 1

2

There is no direct way to handle checked exceptions in lambadas, only option I came up with is just move the logic to another method which can handle it with try-catch.

For example,

List<FileReader> fr = Arrays.asList("a.txt", "b.txt", "c.txt").stream()
            .map(a -> createFileReader(a)).collect(Collectors.toList());
//....
private static FileReader createFileReader(String file) {
    FileReader fr = null;
    try {
        fr = new FileReader(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return fr;
}
Sign up to request clarification or add additional context in comments.

2 Comments

It's probably for the best. This is more readable.
>lambadas best typo ever

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.