https://kotlinlang.org logo
k

Kartik Prakash

08/18/2021, 1:40 PM
Is there any workaround to avoid
@Throws
annotation and handle the error on the native platform side. For example I have function in kotlin native that can throw
SocketTimeoutException
, Is there anyway to handle it on the iOS side without adding the
@Throws
annotation on my function?
m

mkrussel

08/18/2021, 1:55 PM
Convert to using return types like
Result
instead of throwing an exception.
k

Kartik Prakash

08/18/2021, 1:58 PM
yeah that seems like an option. But feels weird to catch all the exceptions the function can potentially throw and wrap it to a return type. @mkrussel anychance you have used
unhandledExceptionHook
before? https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native/set-unhandled-exception-hook.html
m

mkrussel

08/18/2021, 2:13 PM
No. Right now I only use exceptions for programmers bugs and don't handle them. Anything that is expected like IOExceptions, I catch at the point of the IO and convert to result type objects. I feel this is safer, since Kotlin doesn't have checked exceptions. There also aren't that many places where I would have used checked exceptions (pretty much just IO work).
k

Kartik Prakash

08/18/2021, 2:15 PM
gotcha. That makes sense. Thank you for the prompt replies, appreciate it.
2 Views