How to set a breakpoint on all throws? How to set a breakpoint on all throws? xcode xcode

How to set a breakpoint on all throws?


First thing to keep in mind is that errors thrown in Swift are not exceptions, just errors (NSError, whatever based on ErrorType, ...).

Second thing is, don't use try! unless you're damn sure it will not crash or if crash is what you really want.

Don't mix symbolic breakpoint with exception breakpoint. Different beasts.

Back to your question ...

throw is not a symbol, thus symbolic breakpoint doesn't work for you. But there's a way ...

(lldb)br s -E swift-E <language> ( --language-exception <language> )  Set the breakpoint on exceptions thrown by the specified language  (without options, on throw but not catch.)

... this is little bit misleading, because thrown errors are not exceptions. Keep this in mind. When you try to set exception breakpoint in Xcode, there's no Swift. Probably the main reason is that thrown errors are not exceptions and they didn't figure out where to put it yet (who knows).

Exception breakpoint - Edit

Add it manually

Set a breakpoint somewhere in your code, when execution pauses, just type br s -E swift in LLDB prompt and then continue.

Add it automatically

Set a breakpoint somewhere in your code in this way ...

Breakpoint br s -E swift

... and toggle it (on/off) when you do want to stop on throw.

Symbolic breakpoint

When you do use already mentioned br s -E swift you are going to find that there's symbol for throw. Actually it's not throw, but swift_willThrow. Feel free to set symbolic breakpoint in this way ...

swift_willThrow symbolic breakpoint

... I do not recommend this way for now, because it can be changed in the future. But if it's enough for now, why not.

You can share your breakpoint across Xcode projects like this ...

enter image description here

... secondary click, Move Breakpoint To, User. Breakpoint will be visible in all Xcode projects.

When you hit breakpoint, you'll end up in something like this ...

Breakpoint hit on swift_willThrow

... and you have to select previous stack frame to see where the error was thrown ...

Throw stack frame


Just found an alternative in Xcode 8.3: there is a "Swift Error Breakpoint" option in the menu for adding new ad-hoc breakpoints:

swift error breakpoint

If you right-click the newly created breakpoint, you can even specify a particular Error-conforming type to break on.

custom type