When using the use function on autoClosable classe...
# getting-started
k
When using the use function on autoClosable classes that implement close(). Is there a way I can get the suppressed exceptions to log them?
m
The Kotlin Exception class is a type alias to the Java one if you are on JVM. You can then use the
getSuppressed
function on
Throwable
to see anything that was suppressed.
k
so I wrap the use with getSupressed()?
m
No you have to catch the exception (try/catch) and then look at what exception was suppressed (if any) by that exception.
use
will throw any exception thrown by your block or by the
close
function of the
Closable
. If they both throw exceptions, then the one thrown by your code block, will have the one from the
close
added to it as a suppressed exception.
The suppress is a way to report that two exceptions were thrown since only one can actually get thrown by a function.
🙌 1
k
Thanks for a great response 🙂