I want to expand on jonprescott’s answer a little (well, more than a little :-)…
There are three things that folks commonly call exceptions:
Language exceptions, for example, an
NSException
that you throw in Objective-C via @throw
Swift errors, which use a syntax that looks a lot like the syntax used for language exceptions in other languages
Machine exceptions, for example, if you access an address that’s not mapped into your address space
In addition, there are Swift traps, which are machine exceptions raised by the Swift runtime when it detects a problem, for example, unwrapping an optional that’s
nil
[1].
When discussing exceptions you have to clearly distinguish between these types, lest you get very confused. For example, in your specific question you’re asking whether it’s possible to use a language exception facility (
NSSetUncaughtExceptionHandler
) to catch a machine exception (
EXC_BAD_INSTRUCTION
), which is obviously nonsense [2].
There are two mechanisms to handle machine exceptions:
Mach exception handlers
UNIX signals
You can see this dichotomy reflected in your average crash report, which includes both a Mach exception code (for example,
EXC_BAD_INSTRUCTION
) and a UNIX signal (continuing that example,
SIGILL
).
The relationship between these is complex, but the gist is that Mach exceptions are the underlying system used by the kernel which, under specific circumstances, will route the Mach exception through the UNIX signal handling infrastructure to allow for POSIX API compatibility.
Both of these mechanisms are extremely hard to use correctly. For an insight into those difficulties, check out my Implementing Your Own Crash Reporter post.
And that brings us to the inevitable follow-up question: What are you really trying to do? Can you give us some background on your overall goal here?
By way of context, most folks who ask questions like this are trying to achieve some goal that seems simple but is, in reality, stunningly difficult, for example, implementing their own crash reporter.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
[1] I just posted a short list of common causes on this thread.
[2] Although not everyone agrees with me on that point, at least historically. macOS has the ExceptionHandling framework which can catch machine exceptions and convert them to language exceptions. IMO this is utter madness, and I’m very glad it didn’t make the leap to iOS.