异步:如何打破code已经抛出的异常的确切的行? [英] Async: How to break on exact line of code that has thrown the exception?

查看:162
本文介绍了异步:如何打破code已经抛出的异常的确切的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下的code:

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
}

我怎么可以对指定的行的Visual Studio突破的仅在例外是未处理

通常,当异常被抛出时,Visual Studio将打破在这里App.g.i.cs:

Normally when the exception is thrown, Visual Studio will break here in App.g.i.cs:

如果我再启用只是我的code中的调试器选项,这反而打破这里:

If I then enable "Just my code" in the debugger options, it will break here instead:

亲近。如果我再检查 System.Exception的下的例外窗口(按Ctrl + Alt + E)中的时抛出一栏,它会破坏正是我想要的:

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:

但现在Visual Studio将打破所有抛出的异常,而不仅仅是那些未处理。有没有什么办法让Visual Studio中位于c $ C只未处理异常的$最合理的换行,就像它会在常规的非异步code?如果这种行为是不可能的,则:

but now Visual Studio will break on all thrown exceptions, not just unhandled ones. 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? If this behavior is not possible, then:


  • 为什么不可能?

  • 如何才能辨别code已经抛出的异常,不管是一个投行语句的我自己的code的的,或者发生异常的框架方法调用?

  • 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?

我在这里用了3嵌套函数在这个例子中强调的是,我的code可能是复杂设有多家分支机构和嵌套方法调用,并确定code的问题的行是困难的,如果Visual Studio中没有按'在code的最里线T突破。

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.

推荐答案

我想我可能已经发现了回答我的问题。

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

首先,我取消 System.Exception的在例外窗口(按Ctrl + Alt + E)中的时抛出栏下。我这样做是因为我不希望Visual Studio中上的所有的例外,只有的未处理的人。

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.

二,我把只是我的code中的调试器选项启用。

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

现在,当调试器中断,它会破坏这里:

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

按在我的问题的屏幕截图。但是,如果我检查调用堆栈:

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

其实我可以看到异常起源于(在 nested3() 46行)。如果我点击调用堆栈窗口这个条目,Visual Studio将跳转到正确的路线,与本土派窗口甚至会告诉我的本地变量的值在该框架。凉!我想这就是我想要的。

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.

这篇关于异步:如何打破code已经抛出的异常的确切的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆