https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
j

Jiri Bruchanov

02/21/2024, 5:36 PM
Hi, are the iOS exception working in same way as regular jvm/kt one ? I have a code which is throwing a
NSInvalidArgumentException
, but having a test with the call in try/catch statement doesn't catch it 🤔
Copy code
val controller = UIImagePickerController()
runCatching {
     controller.setSourceType(UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera)
}
basically having this code, and the test still failing due to
Copy code
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type 1 not available'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000018048d8a8 __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x000000018008409c objc_exception_throw + 56
	2   UIKitCore                           0x0000000184adebcc -[UIImagePickerController sourceType] + 0
....
j

Jiri Bruchanov

02/25/2024, 1:29 PM
Hi @Pamela Hill thank you for response. I'm not sure that link helps... based on this https://kotlinlang.slack.com/archives/C3PQML5NU/p1708634741339569 there is a
extraOpts = listOf("-Xforeign-exception-mode", "objc-wrap")
but I'm not sure how to apply it for "project" in general... the exception is not from 3rd party (cocoalib) dependency
p

Pamela Hill

02/25/2024, 9:29 PM
Basically, the article says to add @Throws to your functions that possibly could throw this exception if it's a Kotlin exception. Exceptions are like errors in Swift, you can't recover from them and causes a program termination. So the exception/error concept is flipped in Kotlin/Swift.
3 Views