简体   繁体   English

异步:如何打破抛出异常的确切代码行?

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

Say I have the following code: 说我有以下代码:

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

async Task nested1()
{
    await nested2();
}

async Task nested2()
{
    await nested3();
}

async Task nested3()
{
    await Task.Delay(1000);
    throw new Exception("Die");  // <-- I want Visual Studio to break on this line
}

How can I make Visual Studio break on the indicated line only when the exception is unhandled ? 只有在未处理异常时,如何才能使Visual Studio在指定的行中断?


Normally when the exception is thrown, Visual Studio will break here in App.gics: 通常,当抛出异常时,Visual Studio将在App.gics中断:

在此输入图像描述

If I then enable "Just my code" in the debugger options, it will break here instead: 如果我然后在调试器选项中启用“只是我的代码”,它将在此处中断:

在此输入图像描述

Getting close. 越来越接近。 If I then check System.Exception under the "Thrown" column in the Exceptions window (Ctrl+Alt+E), it will break exactly where I want: 如果我然后检查Exceptions窗口(Ctrl + Alt + E)中“Thrown”列下的System.Exception ,它将完全打破我想要的位置:

在此输入图像描述

but now Visual Studio will break on all thrown exceptions, not just unhandled ones . 但现在Visual Studio将打破所有抛出的异常,而不仅仅是未处理的异常 Is there any way to get Visual Studio to break at the most logical line of code only for unhandled exceptions, just like it would in regular non-async code? 有没有办法让Visual Studio只针对未处理的异常打破最合乎逻辑的代码行,就像在常规的非异步代码中一样? If this behavior is not possible, then: 如果无法执行此操作,则:

  • Why is it not possible? 为什么不可能?
  • 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? 如何识别引发异常的代码行,无论是我自己的代码throw语句,还是抛出异常的框架方法调用?

I've used 3 nested functions here in this example to emphasize that my code may be complex with many branches and nested method calls, and identifying the problematic line of code is difficult if Visual Studio doesn't break on the innermost line of code. 我在这个例子中使用了3个嵌套函数来强调我的代码可能很复杂,有许多分支和嵌套方法调用,如果Visual Studio没有破坏最内层的代码行,那么识别有问题的代码行是很困难的。

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). 首先,我在例外窗口(Ctrl + Alt + E)的“Thrown”列下取消选中System.Exception I do this because I don't want Visual Studio to break on all exceptions, only unhandled ones. 我这样做是因为我不希望Visual Studio在所有异常中断,只有未处理的异常。

Second, I keep "Just my code" enabled in the debugger options. 其次,我在调试器选项中启用了“Just my code”。

Now, when the debugger breaks, it will break here: 现在,当调试器中断时,它将在此处中断:

在此输入图像描述

as per the screenshot in my question. 根据我的问题中的截图。 But if I inspect the call stack: 但是,如果我检查调用堆栈:

在此输入图像描述

I can actually see where the exception originated from (in nested3() , line 46). 我实际上可以看到异常的起源(在nested3() ,第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. 如果我在调用堆栈窗口中单击此条目,Visual Studio将跳转到正确的行,“Locals”窗口甚至会显示该框架中局部变量的值。 Cool! 凉! I think this is what I wanted. 我想这就是我想要的。

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. 每次发布时, async仍然会获得更好的工具支持,我确实希望迟早会添加此行为。

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? 如何识别引发异常的代码行,无论是我自己的代码的throw语句,还是抛出异常的框架方法调用?

Visual Studio 2013 does have the ability to view asynchronous causality stacks in the debugger . Visual Studio 2013确实能够在调试器中查看异步因果堆栈 If you want similar behavior at runtime, then you can use my async diagnostics package . 如果您希望在运行时使用类似的行为,则可以使用我的异步诊断程序包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM