Weebly / Try

Handle Objective-C Exceptions with Swift's error handling system

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enhancement Request: support for Swift error handling inside trap block

uriah-eisenstein opened this issue · comments

Using Swift 5 on Xcode 10.2: a Swift function which throws, when called inside a trap block, needs be wrapped in its own do { … try … } catch. This is because trap takes a non-throwing block as an argument.

I encountered this, for instance, with NSManagedObjectContext.save – it is a throwing function in Swift, but may also throw an Objective-C exception in case of invalid data or constraint violation.

(I tried making trap accept a throwing function myself, but it turned out not to be trivial, since Swift doesn't automatically translate an Objective-C block parameter taking NSError** into a Swift throwing function. The following link might be useful, I haven't had time to experiment further: https://stackoverflow.com/questions/49324917/swift-try-inside-objective-c-block)