I like the smell of Swift in the morning…

Debugging a “terminating with uncaught exception of type NSException” crash

Posted: | Author: | Filed under: Swift, Xcode | Tags: | 2 Comments »

From time to time it happens to all of us: An app crashes with the not really helpful message “terminating with uncaught exception of type NSException”. Great.
This can happen when an assertion in a framework you are using fails (among other reasons). When this happens Xcode is not very helpful and just shows you that an exception was thrown. It does not show the reason nor the location where the exception was thrown.

Not very helpful.

To find our more about the exception you can use an Exception Breakpoint:

1. Open the breakpoint navigator (⌘8)
2. Press “+” in the footer
3. Choose “Exception Breakpoint…”
4. Leave all default values. Only change “Action” to “Debugger Command”
5. Enter po $arg1 into the text field

That’s it! Your shiny new Exception Breakpoint should look like this:

Now, when you try to start your app again, it still crashes (obviously), but you’ll see the exception’s message in the Debug area and Xcode shows you the code where the exception was thrown:

Much better! Now you know why your app crashed.