<!--
{
  "availability" : [
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "ExceptionHandling",
  "identifier" : "/documentation/ExceptionHandling/NSExceptionHandler",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Exception Handling"
    ],
    "preciseIdentifier" : "c:objc(cs)NSExceptionHandler"
  },
  "title" : "NSExceptionHandler"
}
-->

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

```
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 <doc://com.apple.documentation/documentation/Foundation/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 <doc://com.apple.documentation/documentation/Foundation/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](/documentation/ExceptionHandling/logging-and-handling-constants)
and [System Hang Constants](/documentation/ExceptionHandling/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](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Exceptions/Tasks/ControllingAppResponse.html#//apple_ref/doc/uid/20000473)
for details.

`NSExceptionHandler` installs its uncaught exception handler using the <doc://com.apple.documentation/documentation/Foundation/NSSetUncaughtExceptionHandler(_:)>
function.

## Topics

### Getting the default exception handler

[`+  defaultExceptionHandler`](/documentation/ExceptionHandling/NSExceptionHandler/default())

Returns the singleton `NSExceptionHandler` instance.

### Getting and setting exception masks

[`-  exceptionHandlingMask`](/documentation/ExceptionHandling/NSExceptionHandler/exceptionHandlingMask())

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

[`-  exceptionHangingMask`](/documentation/ExceptionHandling/NSExceptionHandler/exceptionHangingMask())

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

[`-  setExceptionHandlingMask:`](/documentation/ExceptionHandling/NSExceptionHandler/setExceptionHandlingMask(_:))

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

[`-  setExceptionHangingMask:`](/documentation/ExceptionHandling/NSExceptionHandler/setExceptionHangingMask(_:))

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

### Getting and setting the delegate

[`-  delegate`](/documentation/ExceptionHandling/NSExceptionHandler/delegate())

Returns the delegate of the `NSExceptionHandler` object.

[`-  setDelegate:`](/documentation/ExceptionHandling/NSExceptionHandler/setDelegate(_:))

Sets the delegate of the `NSExceptionHandler` object.

### Constants

[Logging and Handling Constants](/documentation/ExceptionHandling/logging-and-handling-constants)

Use one or more of the following constants in the parameter of [`setExceptionHandlingMask(_:)`](/documentation/ExceptionHandling/NSExceptionHandler/setExceptionHandlingMask(_:))
to specify the types of exceptions that the exception handler should monitor and
whether it should handle or log them.

[System Hang Constants](/documentation/ExceptionHandling/system-hang-constants)

Use one or more of the following constants in the parameter of [`setExceptionHangingMask(_:)`](/documentation/ExceptionHandling/NSExceptionHandler/setExceptionHangingMask(_:))
to specify the types of exceptions that cause the exception to halt execution so
a debugger can be attached.

[Mask Definitions](/documentation/ExceptionHandling/mask-definitions)

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

[Exception Global String Constants](/documentation/ExceptionHandling/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 <doc://com.apple.documentation/documentation/Foundation/NSException/userInfo-swift.property>
dictionary of an <doc://com.apple.documentation/documentation/Foundation/NSException>
object, when requested.

## Relationships

### Conforms To

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Hashable`](/documentation/Swift/Hashable)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Equatable`](/documentation/Swift/Equatable)

[`CVarArg`](/documentation/Swift/CVarArg)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)