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

查看:26
本文介绍了如果(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.WriteLinethrow.出于某种原因,它总是点击 throw.

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

如果我将 throw 移动到它自己的方法中,那么它工作正常.我的问题是它如何忽略 if 块并点击 throw new Exception:

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方法的bug,代码没有实际执行,而是调试器步骤到带有 throw 语句的行.如果在 if 中的 throw 语句之前有一些代码行被忽略,调试器将转到带有 throw<的行/code> 语句.

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天全站免登陆