Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorials:a_simple_way_to_let_xcode_stop_on_an_error [2011/04/08 17:19] – Fixed bold text shilotutorials:a_simple_way_to_let_xcode_stop_on_an_error [2013/03/05 10:19] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +====== A simple way to let Xcode stop on an error ======
  
 +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'':
 +
 +<code objc>
 +NSSetUncaughtExceptionHandler(&onUncaughtException);
 +</code>
 +
 +And right at the beginning of the same file, add the following C-function:
 +
 +<code objc>
 +void onUncaughtException(NSException *exception) 
 +{
 +    NSLog(@"uncaught exception: %@", exception);
 +}
 +</code>
 +
 +Now, every time an exception is thrown that is not handled by any ''@catch''-block, the function above is called. 
 +
 +The magic trick is as simple as can be: **add a breakpoint in this function**. 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!
  tutorials/a_simple_way_to_let_xcode_stop_on_an_error.txt · Last modified: 2013/03/05 10:19 by 127.0.0.1
 
Powered by DokuWiki