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

poohbar

12/06/2020, 4:19 PM
Does it matter whether I extend
Exception
or
RuntimeException
in Kotlin? I thought it doesn't but my
Exception
extension is getting wrapped in
UndeclaredThrowableException
when interacting with a Java library.
v

Vampire

12/06/2020, 6:07 PM
For Java interop there is
@Throws
annotation
t

Tobias Berger

12/07/2020, 8:51 AM
Well, it doesn't really matter in Kotlin (unless you have a catch-Block for explicitly for RuntimeException of course), but the error you get is thrown by Java code. If your Kotlin exception ends up in some Java code, it can make a difference. So if you're wprking with Kotlin exceptions in combination with Java code or libs, it's a good idea to either us RuntimeExceptions or
@Throws(MyException::class)
as suggested by @Vampire (equivalent of adding
throws MyException
to the end of a method signature in Java)
s

spand

12/07/2020, 9:18 AM
Since Kotlin mirrors the Java exception hierarchy I would say you should do the same as you would in Java.
t

Tobias Berger

12/07/2020, 9:24 AM
Kotlin doesn't differentiate between checked and unchecked exceptions, so unless you have Java inter-op there is no need to use RuntimeException
18 Views