So, Kotlin has its own type for Exceptions. Wouldn...
# announcements
n
So, Kotlin has its own type for Exceptions. Wouldn’t it make sense if
Throwable
was an alias for
java.lang.Exception
on the JVM? Anyway, I have run into this problem:
kotlin.NotImplementedError cannot be cast to java.lang.Exception
and I am thinking what is the best way to avoid it. It happens somewhere in my java dependencies (RxJava perhaps). Your thoughts, please?
l
Exceptions and Errors are two kind of Throwables (not the other way around), be it on the JVM in Java or in Kotlin, on all target platforms. Look at the type hierarchy, you'll understand.
Also, you certainly left TODO() calls in your code.
n
Yes, to catch any cases I forgot to implement. But I would like to avoid crashing while casting from one error type to another.
a
I don't think Kotlin has its own types - it's just an "alias" for ease of use. i.e.
kotlin.RuntimeException::class == java.lang.RuntimeException:class
But it's not a
typealias
(I think in older versions it was a
typealias
though)
l
I don't understand why you cast the error types. It's something I never did in over 5 years on JVM/Android programming
k
Look at this for example:

https://www.javamex.com/tutorials/exceptions/ExceptionHierarchy.png

You can't cast an error to an exception.
👍 3
n
@louiscad I cannot locate where this happens in my code. I think it happens in RxJava.
@karelpeeters OK, so if I throw an Exception instead of an Error, I should be fine.
k
Sure.
l
The stacktrace should pin-point it. Regardless, code paths with TODO() calls should be dead code in terms of what can be run realistically in your program
n
@louiscad Nobody disagrees about the
TODO()
calls. Let’s move on.
🆗 1
h
@alex2069 On the JVM,
Exception
still is a `typealias`: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-exception/index.html
a
Ah right yep -
actual
(I was looking at the
expect
definitions which are classes) - makes a lot more sense that it's still a
typealias
on JVM rather than some weird compile-time magic (beyond
typealias
lol)
f
Is there a reason you are specifically casting to
Exception
? Since
Exception
itself does not provide any properties or methods, you are better leaving it as a
Throwable
in this case.
k
He says RxJava is doing that.