Vitali Plagov
09/28/2021, 5:42 PMrunCatching
-equivalent of the try {} finally {}
code in Java?
Is it what the recover{}
function does?Joffrey
09/28/2021, 6:10 PMCloseable
resources there is a use
function. Otherwise it's ad-hoc, AFAIK.hho
09/28/2021, 6:16 PMtry {} finally {}
in Java is try {} finally {}
:
• runCatching
wraps the result in, well, a Result
(which try … finally
doesn't do, especially not in Java, where it's not even an expression).
• recover
is more like a catch (Throwable t) { }
in Java, giving you the opportunity to do something with the exception and still give a Result
backVitali Plagov
09/28/2021, 6:19 PMuse
fun?hho
09/28/2021, 7:03 PMFileWriter("test.txt")
.use { it.write("something") }
Even if the write
fails, the FileWriter
will be closed. Works with every Closable
resource.Vitali Plagov
09/28/2021, 7:24 PMfinally
block is not used on Closable
. So, probably a mis-use of the finally
in my causehho
09/28/2021, 7:29 PMuse
is not applicable to your problem.