0

With Java 8 and streams I do not really get how the exception handling should work when composing several functions?

I would like to write the following:

Arrays.stream((File[]) files).flatMap(file -> Files.lines(file.toPath()));

The compiler says there is an unhandled IOException at "Files.lines(...)"

Putting try catch around this statement does not work and declaring a throws not as well.

micgn
  • 245
  • 1
  • 4
  • 14

1 Answers1

1

You need to catch IOException thrown from Files.lines inside a lambda.

Java 8: Lambda-Streams, Filter by Method with Exception

Community
  • 1
  • 1
  • 1
    Is there any way to use an "inline" try catch expression as mentioned in this other thread? Or do I need to invent a separate uncheck method, which I would not consider very beautiful? – micgn Oct 20 '14 at 14:58