0

I´m using this in a try - catch structure to get informed in which method the exception was thrown:

NSLog(@"%@", NSStringFromSelector(_cmd));

I wonder if there is a way in Xcode 4 also to get the line number where the error occurred.

Any hints?

Ronald Hofmann
  • 1,390
  • 2
  • 15
  • 26

3 Answers3

2

There’s a __LINE__ macro that you might find useful, see this previous question for an inspiration.

Community
  • 1
  • 1
zoul
  • 102,279
  • 44
  • 260
  • 354
1

Try this in the @catch clause:

NSLog(@"Stacktrace: %@", [NSThread callStackSymbols]);

During development you can set a symbolic breakpoint on objc_exception_throw and see where the error occurs in the Xcode debugger.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • …or [use an exception breakpoint](http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4) so you don't have to remember the name `objc_exception_throw`. – rob mayoff Nov 15 '12 at 09:06
  • @robmayoff Depends if the want to invoke the debugger before or after the exception is thrown. – trojanfoe Nov 15 '12 at 09:08
  • By default, an exception breakpoint breaks on throw (in which case it sets a breakpoint on `objc_exception_throw` for you), but you can change it to break on catch (in which case it sets an exception on `__cxa_begin_catch`). Maybe I don't understand what you're getting at. – rob mayoff Nov 15 '12 at 09:12
  • @robmayoff That's interesting and will save me having to look up `objc_exception_throw` every time I want to catch exceptions :) – trojanfoe Nov 15 '12 at 09:13
1

You can add an exception break point in the Break Point Navigator menu in the left side of Xcode and this will stop you at the position when the exception happened

Ahmed Hammad
  • 435
  • 4
  • 8
  • 1
    Yes, I´m aware of that. But I´m thinking of a log file where I can see when errors occurred. Not just for debugging during development. – Ronald Hofmann Nov 15 '12 at 18:47