Title
j

Jaroslav

11/07/2018, 12:59 PM
Is it possible to catch kotlin exception in swift code? (kotlin part imported as a framework)
t

thevery

11/07/2018, 1:17 PM
Yes if cheched
j

Jaroslav

11/07/2018, 1:19 PM
And there is a little complication:
suspend fun <T : Any> call(command: Command<T>): T{
// here exception can be thrown
}

fun <T : Any> callAsync(command: Command<T>, context: CoroutineContext): Deferred<T> {
        return GlobalScope.async(context) {
            call(command = command)
        }
    }
ios runs callAsync, while exception is thrown inside suspend function
t

thevery

11/07/2018, 2:04 PM
You need @Throws annotation to make it work
j

Jaroslav

11/07/2018, 2:05 PM
where? idea doesn’t recognize it 🙂
Unresolved reverence Throws
j

Jaroslav

11/07/2018, 2:26 PM
hmm.. ok, that compiled, thanks!
But IntelliJ doesn’t recognize it
o

olonho

11/07/2018, 2:44 PM
Maybe you have incorrect build script or due to bug your code is not recognized as native module. Can you use other pure native APIs?
s

svyatoslav.scherbina

11/07/2018, 3:18 PM
Kotlin/Native
@Throws
annotation can’t be imported to common code. Instead you can declare it as
expect annotation class
with
actual typealias
.
k

Konstantin Petrukhnov

11/08/2018, 6:31 AM
@svyatoslav.scherbina any code example?
j

Jaroslav

11/08/2018, 6:56 AM
OK, I’ve added @Throws, now objc method has error param and in swift I can use try/catch. But, as actual exception is thrown asynchronously somewhere in the depths of Deferred, it isn’t catched
I’ve now added
invokeOnCompletion(onCancelling:
which receives thrown error, but app crashes nonetheless with the same error
s

svyatoslav.scherbina

11/08/2018, 7:47 AM
@Konstantin Petrukhnov
// Common code:
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR)
expect annotation class Throws(vararg val exceptionClasses: kotlin.reflect.KClass<out kotlin.Throwable>)

// Android code:
actual typealias Throws = kotlin.jvm.Throws

// iOS code:
actual typealias Throws = kotlin.native.Throws
👍 3
@Jaroslav
I’ve now added
invokeOnCompletion(onCancelling:
which receives thrown error, but app crashes nonetheless with the same error
Which one?
j

Jaroslav

11/08/2018, 7:47 AM
ios app
s

svyatoslav.scherbina

11/08/2018, 7:48 AM
Which error?
j

Jaroslav

11/08/2018, 7:50 AM
suspend function throws an exception, then on ios part my callback that was given via
invokeOnCompletion(onCancelling:
get’s called, printing error shows that’s the same one that was thrown, for now everything’s ok, but then after that callback is executed, app crashes with
Uncaught Kotlin exception:
. Setting
NSSetUncaughtExceptionHandler
doesn’t help either.
and crashes with the same error that was printed inside
invokeOnCompletion(onCancelling:
s

svyatoslav.scherbina

11/08/2018, 7:53 AM
Could you please provide exception and crash stacktraces? A sample to reproduce the issue would also be useful.
j

Jaroslav

11/08/2018, 7:59 AM
ok, I’ll try to make a demo app later today
s

svyatoslav.scherbina

11/08/2018, 10:23 AM
@Throws
suspend fun anotherGreeting(): String {
This is wrong.
@Throws
annotation must be applied to the function that is called from Swift directly.
j

Jaroslav

11/08/2018, 10:23 AM
I’ll ad that in a minute, but I’ve tried that before - it won’t help
s

svyatoslav.scherbina

11/08/2018, 10:30 AM
Yes, because
@Throws
annotation must be added to the function that is called from Swift and throws an exception. In your case this function seems to be
deferred.getCompleted()
.
j

Jaroslav

11/08/2018, 10:50 AM
yeah, but deferred is an interface, so even if I override it with mine, globalScope.async will still return original interface that has no
@throws
s

svyatoslav.scherbina

11/08/2018, 10:55 AM
Yes, that’s why it is required to add
@Throws
wrapper for
deferred.getCompleted()
.
j

Jaroslav

11/08/2018, 10:57 AM
sorry, maybe I’m too green in kotlin, but how this can be done?
s

svyatoslav.scherbina

11/08/2018, 10:58 AM
@Throws
fun <T> getCompletedOrThrow(deferred: Deferred<T>): T = deferred.getCompleted()
j

Jaroslav

11/08/2018, 11:41 AM
thanks! I think I’ve got that, finally 🙂
s

svyatoslav.scherbina

11/29/2018, 10:19 AM
You can either apply
@OptionalExpectation
(https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-optional-expectation/index.html) or declare a dummy
actual annotation class Throws
for JS.
:kotlin: 1
a

aakira

11/29/2018, 10:30 AM
Nice annotation! Thank you so much 😄