简体   繁体   中英

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).

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). Hitting F5 will just rerun assertion due to [DebuggerHidden] and "Unwind stack on unhandled exception" setting combo (setting is enabled by default).

To fix the code, just replace the #1 line with object x = "" , set the next statement to it and hit F5 again.

Now, enable "break when thrown" for the ArgumentNullException and uncomment the lines marked with #2. The behavior changes: you're stopped on assertion again, but the stack does not unwind (easy to check with CallStack window). F5 will continue from the place the exception was thrown.

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?

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.

As it works if the exception is not caught down the stack.

UPD2 As proposed by Hans Passant: Posted an suggestion on UserVoice . Feel free to vote:)

and uncomment the lines marked with #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.

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. And SetNext can't work at that point. 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.

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 . Post the URL to your proposal as a commnent and good odds it will get a bunch of votes. 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.

  1. Open the "Exception Settings" Menu (Debug > Windows > Exception Settings)
  2. Under "Common Language Runtime Exceptions", check the box for "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.

However, you cannot edit and continue active statements . If you try to modify your assertion line, you'll see something like this:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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