简体   繁体   English

Visual Studio:编辑并继续处理已处理的异常?

[英]Visual Studio: edit-and-continue on handled exceptions?

Here's a code reproducing the behavior I'm expecting to get: 这是一个代码,再现了我期望得到的行为:

    static void Main(string[] args)
    {
        // try // #2
        {
            string x = null; // #1
            AssertNotNull(x, nameof(x));
        }
        // catch (ArgumentNullException) { } // #2
        Console.WriteLine("Passed.");
        Console.ReadKey();
    }

    [DebuggerHidden]
    public static void AssertNotNull<T>(T arg, string argName) where T : class
    {
        if (arg == null)
            throw new ArgumentNullException(argName);
    }

The behavior is the same for any VS starting from VS2008 (did not check on earlier versions). 对于从VS2008开始的任何VS,其行为都是相同的(未在较早版本上进行检查)。

If you run it under debug (using a standard debugging settings) you're not allowed to continue until you fix the code (using the EnC). 如果在调试下(使用标准调试设置)运行它,则在修复代码(使用EnC)之前,不允许继续运行。 Hitting F5 will just rerun assertion due to [DebuggerHidden] and "Unwind stack on unhandled exception" setting combo (setting is enabled by default). 由于[DebuggerHidden]和“在未处理的异常上展开堆栈”设置组合(默认情况下启用了该设置),因此按F5只会重新运行断言。

To fix the code, just replace the #1 line with object x = "" , set the next statement to it and hit F5 again. 要修复代码,只需将#1行替换为object x = "" ,为其设置下一条语句,然后再次按F5。

Now, enable "break when thrown" for the ArgumentNullException and uncomment the lines marked with #2. 现在,为ArgumentNullException启用“抛出时中断”,并取消标记为#2的行的注释。 The behavior changes: you're stopped on assertion again, but the stack does not unwind (easy to check with CallStack window). 行为发生了变化:您再次在断言时停止,但是堆栈不会展开(可通过CallStack窗口轻松检查)。 F5 will continue from the place the exception was thrown. F5将从引发异常的地方继续。

Ok, so... now the question is: Is there any way to enable auto stack unwinding when breaking on handled exceptions? 好的,所以...现在的问题是:在打破处理的异常时,有什么方法可以启用自动堆栈展开功能吗?

Hidden VS option, existing extension or (maybe) API that can be used from my own extension? 隐藏的VS选项,现有扩展或可以从我自己的扩展中使用的(也许)API?

UPD: To clarify the question: I want to break at the line with failed assertion, edit the code with edit and continue, set next statement to the fixed code and continue the execution. UPD:要澄清一个问题:我想断言失败的行,用edit编辑代码,然后继续,将next语句设置为固定代码,然后继续执行。

As it works if the exception is not caught down the stack. 如果异常未在堆栈中捕获,则可以正常工作。

UPD2 As proposed by Hans Passant: Posted an suggestion on UserVoice . UPD2由Hans Passant提出: 在UserVoice上发布了一条建议 Feel free to vote:) 随意投票:)

and uncomment the lines marked with #2 并取消注释标记为#2的行

This is the critical part of your question. 这是您问题的关键部分。 You changed more than one thing, the critical change is that you altered the way the stack is unwound. 您所做的改变不止一件事,关键的改变是您改变了堆栈的展开方式。 The debugger option you like is titled "Unwind stack on unhandled exception ". 您喜欢的调试器选项的标题为“ 在未处理的异常上展开堆栈”。 Problem is, there is no unhandled exception anymore. 问题是,不再有未处理的异常。 Your catch clause handles it and it is now the CLR that unwinds the stack. 您的catch子句处理它,现在是CLR展开堆栈。

And it must be the CLR that does the unwinding, the debugger does not have an option to do it on the first-chance exception break that you asked for. 并且必须由CLR进行平仓,调试器没有选项可以在您要求的第一次机会异常中断时执行。 And SetNext can't work at that point. 而且SetNext在那时无法正常工作。 Which, if I interpret the question correctly, you would really like to have since what you need to do next is busy work, single stepping through the catch block is not enormous joy. 如果我正确地解释了这个问题,那么您真的很想拥有,因为接下来您需要做的是忙碌的工作,单步执行catch块并不会带来很大的乐趣。

Although it is not implemented, I think it is technically do-able. 尽管尚未实施,但我认为它在技术上是可行的。 But only because I'm blissfully unaware how much work the debugger team will have to do. 但这仅仅是因为我非常高兴地不知道调试器团队将要做多少工作。 It is a good ask to make E+C work better, you can propose it here . 使E + C更好地工作是一个很好的要求,您可以在此处提出 Post the URL to your proposal as a commnent and good odds it will get a bunch of votes. 将URL发布到您的提案中作为一个好主意,很可能会赢得很多选票。 I'll vote for it. 我会投票。

To clarify the question: I want to break at the line with failed assertion, edit the code with edit and continue, set next statement to the fixed code and continue the execution. 为了澄清这个问题:我想断言失败的行,用edit编辑代码,然后继续,将next语句设置为固定代码,然后继续执行。

  1. Open the "Exception Settings" Menu (Debug > Windows > Exception Settings) 打开“例外设置”菜单(“调试”>“ Windows”>“例外设置”)
  2. Under "Common Language Runtime Exceptions", check the box for "System.ArgumentNullException". 在“公共语言运行时异常”下,选中“ System.ArgumentNullException”框。 (Or check them all, whatever you're looking for.) (或者全部检查,无论您要寻找什么。)

It should now break whenever a System.ArgumentNullException is thrown, regardless if it will be caught in a catch block. 现在,无论何时抛出System.ArgumentNullException ,它都应该中断,无论是否将其捕获在catch块中。

However, you cannot edit and continue active statements . 但是, 您不能编辑和继续活动语句 If you try to modify your assertion line, you'll see something like this: 如果您尝试修改断言行,则会看到类似以下内容的内容:

在此处输入图片说明

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

相关问题 Visual Studio 2015意外地破坏了处理的异常 - Visual Studio 2015 unexpectedly breaking on handled exceptions 在Visual Studio中平滑编辑并继续 - Smooth Edit & Continue in Visual Studio 避免或接受破坏编辑和继续的C#构造? - Avoid or embrace C# constructs which break edit-and-continue? Visual Studio 2017中断异常(也处理异常) - Visual studio 2017 break on throw for exceptions (also handled exceptions) 有没有计划在未来的VS版本中支持XAML的编辑和继续? - Are there any plans to support edit-and-continue of XAML in future VS versions? 如何在Visual Studio中启用“编辑并继续” - How to enable Edit and Continue in Visual Studio Visual Studio + Docker-使用“编辑并继续”调试应用程序吗? - Visual Studio + Docker - Debugging applications with Edit and Continue? Visual Studio 2015中缺少“编辑并继续”选项 - “Edit and Continue” option is missing on Visual Studio 2015 Visual Studio 2013“打破处理的异常”不起作用,而不是破坏 - Visual Studio 2013 “break on handled exceptions” not working, not breaking 如何让 Visual Studio 2015 忽略处理的异常? - How do I make Visual Studio 2015 ignore handled exceptions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM