Tuesday, January 31, 2017

One More Step Towards Swift Developers

The Swift language is designed to be as interoperable as possible with Objective-C. This goal is generally fulfilled, but an area still remains where the two languages don't interoperate as well as one would expect: exception handling.

Objective-C provides complete try-catch-finally semantics for exception handling, with full stack unwinding. While Cocoa and CocoaTouch conventions discourage their use for runtime errors (originally for performance reasons), they are of common use also in Apple's frameworks for programmer errors (i.e. logic errors, such as accessing an out-of-bounds element of an NSArray).

Swift, on the other hand, provides similar do-try-catch semantics, but its errors (do not call them exceptions) are actually passed back as an out parameter and does not provide a stack unwinding mechanism. Moreover, as of Swift 3.x, Objective-C exceptions are left completely out of Swift error handling: they are uncatchable in this language, and cause a direct crash of the application or the service. For the rationale behind this choice, and its corresponding discussion, see this dissertation on the Swift GitHub repository.