Is there any way how we can specify a breakpoint to stop on all 'throw' statements? (symbolic breakpoint)
guard let id = UInt(idString),
let changeset = UInt(changesetString),
let uid = UInt(uidString)
else {
throw OSMVectorMapDescriptionError.ElementAttributeConversionError(element: xmlNode, attributeº: nil)
}
Just found an alternative in Xcode 8.3: there is a "Swift Error Breakpoint" option in the menu for adding new ad-hoc breakpoints:
If you right-click the newly created breakpoint, you can even specify a particular Error
-conforming type to break on.
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).
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 ...
... 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 ...
... 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 ...
... 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 ...
... and you have to select previous stack frame to see where the error was thrown ...