https://kotlinlang.org logo
#getting-started
Title
# getting-started
k

Kristian Nedrevold

09/13/2022, 8:20 PM
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

mkrussel

09/13/2022, 8:43 PM
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

Kristian Nedrevold

09/13/2022, 8:59 PM
so I wrap the use with getSupressed()?
m

mkrussel

09/13/2022, 9:28 PM
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

Kristian Nedrevold

09/14/2022, 10:28 AM
Thanks for a great response 🙂
12 Views