Breaking on Exceptions in XCode

If you happen to be experienced in development using Visual Studio, but a bit new to working in XCode for iOS development (like me), you are probably used to your application breaking on exceptions during debugging.  But by default, your iOS application may not act as you expect when it comes to debugging crash exceptions.  You may instead see something like this:


testbreak.xcodeproj — main.m

With an error message that says: 

*** Terminating app due to uncaught exception


What?  Why was my exception uncaught while I am debugging?  I, for one, would rather be taken to the point at which the exception occurred, rather than thrown out in my main.m file.

There is actually an easy fix here, and also a lesson in using XCode itself.  Train yourself to look not only at the top of window panes for menus and options, but also at the bottom.  Because if you look at the bottom of the Breakpoint Navigator in XCode, you will see some additional - and very useful - buttons for adding, deleting, and filtering your breakpoints.

testbreak.xcodeproj — main.m-1

 

Use the + button to add an 'Exception Breakpoint', and set it to break on throw for all exceptions.

testbreak.xcodeproj — main.m-2 Xcode(1)

There!  Now execution will break and take you to the location of the problematic code (an unrecognized selector, in this case), instead of main.m.

After all, we can't fix our bugs until we know where they are!  Just don't forget you will need to do this for all of your projects since it is actually a breakpoint in the project and not an XCode setting.

testbreak.xcodeproj — FSViewController.m

comments powered by Disqus