methods that throws checked exceptions in a lambda block
This line does not compile Files.list(Paths.get("/secret")).forEach(x -> Files.write(x, new byte[Files.size(x)]); because Files.write() and Files.size() throw IOE. Then what's the best way to make it work? (I find some old threads on this on lambda-dev. Maybe we have a recommended way now?) Thanks Max
On 02/12/2014 07:05 AM, Wang Weijun wrote:
This line does not compile
Files.list(Paths.get("/secret")).forEach(x -> Files.write(x, new byte[Files.size(x)]);
because Files.write() and Files.size() throw IOE. Then what's the best way to make it work?
(I find some old threads on this on lambda-dev. Maybe we have a recommended way now?)
I am not sure there is a "recommended" way. Depends on how you need to handle those exceptions, I'll put the lambda in a private function to take care those exception, and surpress them in a UncheckedIOException. Cheers, Henry
On 12/02/2014 16:36, Henry Jen wrote:
I am not sure there is a "recommended" way. Depends on how you need to handle those exceptions, I'll put the lambda in a private function to take care those exception, and surpress them in a UncheckedIOException.
Yes, I think it depends on whether he wants to recover and continue to attempt to zero the remaining files or not. -Alan
On 02/12/2014 05:36 PM, Henry Jen wrote:
On 02/12/2014 07:05 AM, Wang Weijun wrote:
This line does not compile
Files.list(Paths.get("/secret")).forEach(x -> Files.write(x, new byte[Files.size(x)]);
because Files.write() and Files.size() throw IOE. Then what's the best way to make it work?
(I find some old threads on this on lambda-dev. Maybe we have a recommended way now?)
I am not sure there is a "recommended" way. Depends on how you need to handle those exceptions, I'll put the lambda in a private function to take care those exception, and surpress them in a UncheckedIOException.
Cheers, Henry
The other solution is to use wrappers like these ones: https://github.com/forax/blog/blob/master/src/com/github/forax/blog/UnsafeIO... and rewrite your code like this: Files.list(Paths.get("/secret")).forEach(unsafeProc(x -> Files.write(x, new byte[Files.size(x)])); cheers, Rémi
On Feb 12, 2014, at 4:05 PM, Wang Weijun <weijun.wang@oracle.com> wrote:
This line does not compile
Files.list(Paths.get("/secret")).forEach(x -> Files.write(x, new byte[Files.size(x)]);
because Files.write() and Files.size() throw IOE. Then what's the best way to make it work?
It depends... Sometimes i will shuffle things under a method (static one if there are no captures and use a method ref). If i get *really* fed up i have been know to write stuff like the following: private static interface S<T, E extends IOException> { T apply() throws E; } private static <T, E extends IOException> T r(S<T, E> s) { try { return s.apply(); } catch (IOException t) { throw new UncheckedIOException(t); } } Paul.
(I find some old threads on this on lambda-dev. Maybe we have a recommended way now?)
participants (5)
-
Alan Bateman
-
Henry Jen
-
Paul Sandoz
-
Remi Forax
-
Wang Weijun