[swift-evolution] Catching NSException

Jon Brooks jbrooks512 at gmail.com
Thu Mar 24 13:06:03 CDT 2016


Apologies if this has come up before - I've fallen behind in following this
list.

I recently ran into an issue where I needed to be able to catch
NSExceptions raised by Objective C API in Swift, and found no good way to
do that.  Currently the only possible way is to via Objective C code that
wraps the call in an Objective C style @try/@catch block.  If building a
swift framework, this means a separate module, since we can't use bridging
headers.

My quick attempt at a workaround can be seen here:
https://github.com/jonbrooks/ObjCTryCatch and there are other workarounds
out there too.  I wondered if there has been any discussion to building
something like this into swift directly.  I don't really have any good
ideas, but maybe something like

    do {
        objc_try someObjectiveCInstance.methodThatMightRaiseException()
    } catch {
        //error would be an ErrorType that contains info about the
exception raised, or the exception itself?
    }

Any thoughts?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160324/941ba7e7/attachment.html>


More information about the swift-evolution mailing list