Async: How to break on exact line of code that has thrown the exception?

前端 未结 2 1609
星月不相逢
星月不相逢 2021-01-18 06:36

Say I have the following code:

async void buttonClick(object sender, RoutedEventArgs e)
{
    await nested1();
}

async Task nested1()
{
    await nested2();         


        
2条回答
  •  不要未来只要你来
    2021-01-18 07:32

    I think I may have discovered the answer to my question.

    First, I uncheck System.Exception under the "Thrown" column in the exceptions window (Ctrl+Alt+E). I do this because I don't want Visual Studio to break on all exceptions, only unhandled ones.

    Second, I keep "Just my code" enabled in the debugger options.

    Now, when the debugger breaks, it will break here:

    enter image description here

    as per the screenshot in my question. But if I inspect the call stack:

    enter image description here

    I can actually see where the exception originated from (in nested3(), line 46). If I click this entry in the call stack window, Visual Studio will jump to the correct line, and the "Locals" window will even show me the values of local variables in that frame. Cool! I think this is what I wanted.

提交回复
热议问题