Does it matter whether I extend `Exception` or `Ru...
# getting-started
p
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
For Java interop there is
@Throws
annotation
t
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
Since Kotlin mirrors the Java exception hierarchy I would say you should do the same as you would in Java.
t
Kotlin doesn't differentiate between checked and unchecked exceptions, so unless you have Java inter-op there is no need to use RuntimeException