show line of code causing error in debug

When I debug an error I end up with either the line of code causing the error or a dump with lots of things that are not of much help to me. Is there a way to force the line of code causing the error to show up?

There's not always a well-defined "line of code causing the error", and it depends a bit on what kind of errors you're talking about, but the usual thing is to set an exception breakpoint (using the "+" button menu at the bottom of the Breakpoint navigator).


In the case of an Obj-C exception (NSException), this will stop just before the error is logged (usually), but the line of the error is somewhere up the backtrace/call stack.


However, in rare cases, frameworks written in C++ internally will use C++ exceptions for flow control rather than unrecoverable errors, so it's usually worth configuring the exception breakpoint for Obj-C only.


For Swift, there are no exceptions beyond the Obj-C exceptions thrown in the code you call, but you can set an error breakpoint which will trigger on a thrown Swift error. However, there are probably a lot of errors that are recovered, so you probably want to configure the breakpoint options for a specific error value, once you've observed it happening.


So, you have several tools available here, but there's no simple answer about how best to use them.


Finally, keep in mind that some Obj-C exceptions are thrown asynchronously (that is, later than the code that really caused them). For example, NSTableView defers some of the things it does until the next iteration of the run loop, so sometimes you can tell it to do something invalid, and the error shows up in what appears to be system code.

That line is typically where the error occured in the flow - not where the actual code is that root caused the error. Discussion here: https://stackoverflow.com/questions/35346729/how-to-find-which-line-error-occurs-in-xcode


Xcode isn't that helpful outright, but if you learn more about debugging, break points, stack trace etc. you can improve your skills to help isolate where you need to focus your attention.


If you haven't seen the docs yet:

https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/debugging_with_xcode/chapters/debugging_tools.html


Good luck.

show line of code causing error in debug
 
 
Q