A simple way to let Xcode stop on an error

Daniel Sperl on April 7, 2010

This post is not Sparrow-specific, but I thought this could come in handy for any Xcode developer.

Developing an application is always more or less trial and error. You write some code. You test it. You write more code. You test it. This goes on for a long while. During this process, even the best of us make lots of mistakes - everybody does, and it does not matter. That’s just how programming works. Don’t believe anybody who tries to tell you it’s different.

However, when those errors happen, we are happy to get to the point where the error occurred (in other words: where an exception was thrown) as quickly as possible. We don’t want a cryptic log message; we want the IDE to stop at the exact point where the error happened.

Fortunately, this is possible. Not for each and every error, but at least for quite of lot of them. You just have to use NSSetUncaughtExceptionHandler. It’s easy to do, really. Just add this line of code right at the beginning of the applicationDidFinishLaunching method of your ApplicationDelegate:

NSSetUncaughtExceptionHandler(&onUncaughtException);

And right at the beginning of the same file, add the following C-method:

void onUncaughtException(NSException *exception) 
{
    NSLog(@"uncaught exception: %@", exception.description);
}

Now, every time an exception is thrown that is not handled by any @catch-block, the method above is called.

The magic trick is as simple as can be: add a breakpoint in this method. When an error occurs, the debugger will stop at the breakpoint, and you can examine the call stack to find out where exactly the exception was created. (For those new to Xcode, the call stack is hidden on the right side of the small debugging toolbar that is shown above the source code while you are in debug mode.)

Unfortunately, not every error is a clean “Exception” (e.g. memory errors are not, which is a bummer), so it won’t work in all cases. But every time it does work, it’s good to have made this little preparation.

I hope this helps some of you in your coding sessions!