NSException class provides facilities for monitoring and debugging exceptional conditions in Objective-C programs. It works by installing a special uncaught exception handler via the
NSSet function. Consequently, to use the services of
NSException, you must not install your own custom uncaught exception handler.
- macOS 10.0+
To use these services, you set a bit mask in the singleton
NSException instance and, optionally, a delegate. The constants comprising the bit mask indicate the type of exception to be monitored and the behavior of the
NSException object (or, simply, the exception handler). The delegate is asked to approve the logging and handling of each monitored
NSException object using the NSExceptionHandlerDelegate protocol.
The constants for configuring exception handler behavior can be categorized in several ways:
Uncaught exceptions versus caught exceptions—or, more accurately, exceptions that would be caught (for example, by the top-level handler)
Exception type or cause: system exceptions (such as invalid memory accesses), Objective-C runtime errors (such as messages sent to freed objects), and other exceptions
Exception handler behavior: logging the exception (including a stack trace) to the console, handling the exception, and suspending program execution so the debugger can be attached
The way the exception handler handles an exception depends on the type of exception; the exception handler converts system exceptions and runtime errors into
NSException objects with a stack trace embedded in their
user dictionary; for all other uncaught exceptions, it terminates the thread on which they occur . The constants used to configure an
NSException object are described in Logging and Handling Constants and System Hang Constants.
defaults command-line system also allows you to set values corresponding to the
enum constants used to configure the exception handler; see Controlling a Program’s Response to Exceptions for details.