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

前端 未结 2 1584
星月不相逢
星月不相逢 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条回答
  • 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.

    0 讨论(0)
  • 2021-01-18 07:32

    It's not possible.

    Why is it not possible?

    Because it's not done yet. async is still getting better tooling support with every release, and I do expect this behavior to be added sooner or later.

    How can I identify the line of code that has thrown the exception, whether that be a throw statement of my own code, or a framework method call that has thrown an exception?

    Visual Studio 2013 does have the ability to view asynchronous causality stacks in the debugger. If you want similar behavior at runtime, then you can use my async diagnostics package.

    0 讨论(0)
提交回复
热议问题