简体   繁体   English

为什么调试器会遇到默认情况?

[英]Why does the debugger hit the default case?

Some colleague ran into a very strange debugging issue in which he steps through a switch, steps to some case but yet afterwards it hits the default case as well.一些同事遇到了一个非常奇怪的调试问题,他单步执行开关,单步执行某些情况,但随后又遇到了默认情况。 I have no idea how that can ever happen!我不知道这怎么会发生!

It's easily reproducible and I finally have extracted the code into a small test.它很容易重现,我终于将代码提取到一个小测试中。

If I put a breakpoint on the default case only, then the debugger will never stop there.如果我只在默认情况下设置断点,那么调试器将永远不会停在那里。 But If I add a breakpoint at case 42, and then use step over, it will hit the default case afterwards!!但是如果我在case 42处加一个断点,然后使用step over,它之后会碰到default case!! Remark that the catch is never reached though.请注意,尽管从未达到过捕获。 So it seems that the debugger is showing something that is not actually happening?所以调试器似乎显示了一些实际上没有发生的事情? Also, if I remove the async task and await, then it will not happen.. We also need another case that declares a variable (something fancy, not a simple integer etc..)另外,如果我删除异步任务并等待,那么它就不会发生..我们还需要另一个声明变量的案例(花哨的东西,而不是简单的整数等。)

public class Foo
{
    public readonly string Bar = nameof(Bar);
}

public async Task DoSomething(int x)
{
    try
    {
       switch (x)
       {
            case 43:
                var foo = new Foo();
                break;     
            case 42:
                Console.WriteLine(x);
                break;
            default:
                throw new NotImplementedException("aaarghh..");
        }
    }
    catch (Exception exc)
    {
        MessageBox.Show(exc.Message);
    }
}

private async void btnSomethingTo_Click(object sender, EventArgs e)
{
    await DoSomething(42);
}

GIF created by Sinatr to illustrate the topic on hand, thanks! Sinatr 创建的 GIF 用于说明手头的主题,谢谢!手头的问题

You have used the DoSomething method not in proper way.您没有以正确的方式使用DoSomething方法。 ASYNC keyword means you have some await call somewhere within method (to get return point later after task end), but you haven't it. ASYNC关键字意味着您在方法中的某处有一些await调用(以便在任务结束后稍后获得返回点),但您没有。 Your strange debugger behavior is related to implementation.您奇怪的调试器行为与实现有关。 Your exception is not thrown by fact.你的异常不是事实抛出的。 I have reproduced your case and MyException constructor is just ignored/skipped by debugger, and the code in catch (Exception exc) section is never called too.我已经复制了您的案例,并且调试器只是忽略/跳过了MyException构造函数,并且从未调用过catch (Exception exc)部分中的代码。 Or if I am got break within CASE 42: and I just press F5, then my breakpoint within DEFAULT section will be skipped.或者,如果我在CASE 42 中出现中断并且我只是按 F5,那么我在 DEFAULT 部分中的断点将被跳过。

public async Task DoSomething(int x)
        {
            try
            {
                switch (x)
                {
                    case 43:
                        var foo = new Foo();
                        break;
                    case 42:
                        Debug.WriteLine(x);
                        break;
                    case 44:
                        break;
                    default:
                        throw new MyException("aaarghh..");
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            await DoSomething(42);
        }

        public class MyException : Exception
        {
            public MyException(string message): base(message)
            {
                if (message != null)
                    Debug.WriteLine(message);
            }

            public MyException() : base()
            {
            }

            public MyException(string message, Exception innerException) : base(message, innerException)
            {
            }
        }

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

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