Contents

NSExceptionHandler

The NSExceptionHandler class provides facilities for monitoring and debugging exceptional conditions in Objective-C programs. It works by installing a special uncaught exception handler function. Consequently, to use the services of NSExceptionHandler, you must not install your own custom uncaught exception handler.

Declaration

class NSExceptionHandler

Overview

To use these services, you set a bit mask in the singleton NSExceptionHandler instance and, optionally, a delegate. The constants comprising the bit mask indicate the type of exception to be monitored and the behavior of the NSExceptionHandler 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 userInfo dictionary; for all other uncaught exceptions, it terminates the thread on which they occur . The constants used to configure an NSExceptionHandler object are described in Logging and Handling Constants and System Hang Constants.

The 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.

NSExceptionHandler installs its uncaught exception handler using the NSSetUncaughtExceptionHandler(_:) function.

Topics

Getting the default exception handler

Getting and setting exception masks

Getting and setting the delegate

Constants