Lihai Feng
Lihai Feng

Reputation: 31

Why can't we directly catch exceptions generated by Kotlin code in oc or swift?

Why can't we directly catch exceptions generated by Kotlin code in oc or swift? Is it because of what processing has Kotlin done?

Upvotes: 3

Views: 1019

Answers (1)

Kevin Galligan
Kevin Galligan

Reputation: 17312

Exception handling in Swift and Objc is different than what you're used to on the JVM and in Kotlin. That's just something you need to get used to. On the JVM, you can just "catch everything" at a higher level, but in Swift and Objc, you need to tell the caller that you may be throwing an exception, and the caller must call with a try/catch.

Kotlin got rid of checked exceptions. Swift/Objc are on the other end of the spectrum in that there are only explicit exceptions, and you must call them with a try block.

You can add the @Throws annotation to a method that you're calling directly from Swift/Objc, and that will throw exceptions in the way Swift/Objc expect. However, if you don't annotate that methods with @Throws, the app will abort instead.

Upvotes: 4

Related Questions