can someone help me understand this section under the code? are they explaining the reasoning behind using the generic Exception class?
j
Jeff Lockhart
10/24/2022, 3:45 PM
Yes. See docs. Because Kotlin doesn't have checked exceptions, but Swift only has checked exceptions, in order for exceptions to be caught in Kotlin and propagated in Swift as a checked exception, you have to declare the exceptions with a
@Throws
annotation, with the exception types that Kotlin should catch and propagate as checked exceptions. In this case, using
Exception::class
tells Kotlin to catch and propagate all
Exception
types as checked exceptions in Swift.
o
oday
10/24/2022, 3:47 PM
Ah so the type of exception can be whatever I want
oday
10/24/2022, 3:47 PM
@Throws is the mandatory part for iOS
j
Jeff Lockhart
10/24/2022, 3:48 PM
Yes. Any exception type not included in the annotation won't propagate as checked and will terminate the program.
o
oday
10/24/2022, 3:49 PM
Oh it will outright crash when on iOS as if you didn’t handle it, cause not propagated, got it thanks
j
Jeff Lockhart
10/24/2022, 3:50 PM
As a
suspend
function, it will propagate
CancellationException
automatically, without the explicit annotation. But any other expected exception type needs to be declared.