Is there a way to automatically catch any of swift...
# kotlin-native
v
Is there a way to automatically catch any of swift/objc exceptions/errors in
try ... catch ...
in Kotlin while calling
interface
method implemented in swift/objc? I saw relatively new
ForeignException
but afaik it only works for cinteroped libraries with special flag
s
ObjC errors aren’t thrown and caught like Java Exceptions. An error pointer is passed into the function and if after calling the function, it has a value, then the function has generated an error. Swift adds some syntactic sugar around this to make it look more like a traditional try catch but it is still fundamentally the same as the way ObjC errors are done. There are exceptions in ObjC that can be thrown but they’re meant for unrecoverable cases. If an ObjC exception is thrown, the program should gracefully shutdown quickly.
v
An error pointer is passed into the function and if after calling the function, it has a value, then the function has generated an error.
I’m getting the idea that Kotlin-ObjC interop layer could do this implicitly and rethrow as Kotlin exception if the error pointer has the value. Please correct me if I’m getting a wrong impression
By the way it’s debatable that the program should quit ASAP on
NSException
. For instance, you can take XCTest framework. It throws
_XCTestCaseInterruptionException
when assertion fails as the means to stop execution of current test. If your test is written partly in Kotlin and uses XCTest assertions then the whole process just aborts instead of marking test failure and let other test execute.
c
Hi, I’m trying to get past this issue where an NSException is thrown from a pod library used in kotlin native. It happens when I run tests and since it terminates the program, the tests itself won’t finish and the other after that one won’t start. I’m using Kotlin 1.8.21 and following the 1.4.20 docs I made a gradle task that on the correct
DefFileTask
appends
foreignExceptionMode = objc-wrap
. Though when I run the tests the behaviour remains unchanged and the whole program crashes. I can see that foreign exception is still supported on the master branch from kotlin/native and is not marked as deprecated, so I would expect it to work. Here is some code:
Copy code
// kotlin wrapper for the obj-c library

@Throws(ForeignException::class)
actual fun wrapperMethod(par1: String, par2: (par1_1: Par1_1, par2_2: Par2_2) -> Unit) {
    MethodFromObjCLibrary(par1, par2) // inside here is where the NSException eventually is called. I tried to wrap it inside a try/catch, but didn't work.
}

// swift tests
wrapperMethod("Something") { par1_1, par2_2 in
    // do something with XCTests
}
Now I’m not sure whether I understood how this flag should work and the documentation on that is very poor. Did you guys solved the issues you were having? If yes (or no), can you point a finger for me in the right direction? Thanks in advance!
For instance, you can take XCTest framework. It throws
_XCTestCaseInterruptionException
when assertion fails as the means to stop execution of current test.