如果(false == true)内部抛出异常时执行块 [英] If (false == true) executes block when throwing exception is inside

查看:109
本文介绍了如果(false == true)内部抛出异常时执行块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个很奇怪的问题。

I have a rather strange problem that is occurring.

这是我的代码:

private async Task BreakExpectedLogic()
{
    bool test = false;
    if (test == true)
    {
        Console.WriteLine("Hello!");
        throw new Exception("BAD HASH!");
    }
}

似乎很简单,不应该按 Console.WriteLine throw
由于某种原因,它总是会击中

Seems really simple, it shouldn't hit the Console.WriteLine or the throw. For some reason it's always hitting the throw.

如果我移动到自己的方法,然后就可以正常工作了。我的问题是它如何忽略 if 块并点击引发新的异常

If I move the throw into its own method then it works fine. My question is how is it ignoring the if block and hitting the throw new Exception:

编辑1:我已经更新了我的包含签名的代码,我已经删除了与该问题无关的所有内容,然后运行它,它仍然会发生。

EDIT 1: I've updated my code to include the signature, I've removed everything not related to this problem and ran it, it still happens.

推荐答案

它似乎是 async 方法中的错误,实际上未执行代码 ,但是调试器使用 throw 语句。如果在内的抛出语句之前有一些代码行,如果这些行被忽略,则调试程序仅执行 到带有 throw 语句的行。

It seems to be the bug in async method, the code is not actually executed but debugger steps to the line with throw statement. If there are some lines of code before throw statement inside if these lines are ignored, debugger steps only to the line with throw statement.

此外,如果您不使用变量- if(false) if(true == false),然后调试器执行正确的代码行-结束卷曲

Also, if you don't use variable - if (false) or if (true == false) then debugger steps to the correct line of code - to the closing curly brace.

此错误已由 @Matthew Watson 发布到Visual Studio团队(链接现在不可用)。

This bug has been posted by @Matthew Watson to Visual Studio team (link is not available now).

另外,请参见类似的问题-条件签入异步方法

Also, see similar question - Condition check in async method

编辑(2017/10/06):

问题不能使用.Net Framework 4.7在VS 2017 15.3.5中复制。好像VS团队已解决此问题。

Issue cannot be reproduced in VS 2017 15.3.5 using .Net Framework 4.7. Seems like VS team has fixed this issue.

这篇关于如果(false == true)内部抛出异常时执行块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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