Handle Uncaught Exception at SDK level (Swift SDK for Mobile devices)

I developed a Swift SDK where i need to handle all uncaught exceptions and crashes with in SDK.

My objective is simple would like to capture uncaught exception with the call stack and log to file or send to server.


I would like to know, is there any way to capture uncaught exceptions with in my swift SDK?

or any 3rd party library which will help me to integrate only with SDK to capture the crashes or uncaught exceptions?


Thanks in advance

Replies

If you're trying to do this to catch problems that originate from within your SDK, Swift doesn't throw exceptions. Error handling invoking try / catch statements are not the same as exception handling, and don't unwind call stacks. Please see the language guide for more about error handling in Swift.


If you're looking to catch exceptions thrown from anywhere in an app integrating your SDK, I strongly encourage you to not do this, as that amounts to writing your own crash reporter, which is not recommended. For background on why you shouldn't do this, please see this forums thread.

Thanks edford for the clarification.
how will we capture if any uncaught exception or crash like SIGABRT, SIGTRAP, etc... ?
any suggestion from your end for either application level or SDK level to capture the above signals?

how will we capture … crash like

SIGABRT
,
SIGTRAP
, etc... ?

There is no safe way to catch these signals in Swift. Read the post that Ed referenced, Implementing Your Own Crash Reporter, which has a Signals section that covers this topic specifically. To quote:

This means that a crash reporter’s signal handler cannot use Objective-C or Swift, as there’s no way to constrain how those language runtimes allocate memory.

And then read the rest of that post, which explains all the other reasons why it’s impossible to implement a reliable crash reporter from within your own process.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"