Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a breakpoint on all throws?

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)
}
like image 606
ambientlight Avatar asked Jul 23 '15 02:07

ambientlight


People also ask

How do I add a breakpoint to all methods in eclipse?

Select all the methods using ctrl . Right click and select Toggle Method Breakpoint .

Where do you put a breakpoint?

To set a breakpoint in source code: Click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.

How do you set a breakpoint in C++?

To set a function breakpoint, on the Run view right-click inside the Breakpoints section, then choose Add Function Breakpoint and enter the name of the function on which you want to break execution.

How do you create a symbolic breakpoint?

In the Breakpoint navigator, click the Add button (+) in the lower-left corner, and choose Symbolic Breakpoint. Enter the object and symbol in the Symbol field, using the format of the example text. The debugger pauses when the app or your code calls the symbol you specify.


2 Answers

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

like image 55
zrzka Avatar answered Oct 02 '22 00:10

zrzka


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

like image 42
Max Chuquimia Avatar answered Oct 02 '22 01:10

Max Chuquimia