Class

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 via the NSSetUncaughtExceptionHandler function. Consequently, to use the services of NSExceptionHandler, you must not install your own custom uncaught exception handler.

Declaration

@interface NSExceptionHandler : NSObject

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.

Topics

Getting the default exception handler

+ defaultExceptionHandler

Returns the singleton NSExceptionHandler instance.

Getting and setting exception masks

- exceptionHandlingMask

Returns a bit mask representing the types of exceptions monitored by the receiver and its handling and logging behavior.

- exceptionHangingMask

Returns a bit mask representing the types of exceptions that will halt execution for debugging.

- setExceptionHandlingMask:

Sets the bit mask of constants specifying the types of exceptions monitored by the receiver and its handling and logging behavior.

- setExceptionHangingMask:

Sets the bit mask of constants specifying the types of exceptions that will halt execution for debugging.

Getting and setting the delegate

- delegate

Returns the delegate of the NSExceptionHandler object.

- setDelegate:

Sets the delegate of the NSExceptionHandler object.

Constants

Logging and Handling Constants

Use one or more of the following constants in the parameter of setExceptionHandlingMask: to specify the types of exceptions that the exception handler should monitor and whether it should handle or log them.

System Hang Constants

Use one or more of the following constants in the parameter of setExceptionHangingMask: to specify the types of exceptions that cause the exception to halt execution so a debugger can be attached.

Mask Definitions

The following #define constants are conveniences for specifying complete sets of exception-handling enum constants.

Exception Global String Constants

Two of the following global string constants identify exceptions generated by the framework for Objective-C runtime errors and system exceptions such as invalid memory accesses. The other constant is used as a key to access the stack trace in the userInfo dictionary of an NSException object, when requested.

Relationships

Inherits From