如何使 [DebuggerNonUserCode] 在简单的测试用例中对调试器隐藏异常? [英] How to make [DebuggerNonUserCode] hide an exception from the debugger in simple test case?

查看:10
本文介绍了如何使 [DebuggerNonUserCode] 在简单的测试用例中对调试器隐藏异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:

1) MSVS 2015,选项 -> 调试器 -> 选中仅我的代码".

1) MSVS 2015, Option -> Debugger -> "Just My Code" is checked.

2) 此示例代码放置在某个类中并在启动期间调用:

2) This sample code placed within some class and called during startup:

    static bool TestIgnoreException()
    {
        Exception ex;
        return TrySomething(out ex);
    }

    [DebuggerNonUserCode] // Prevent exceptions from stopping the debugger within this method. 
    static bool TrySomething(out Exception exOut)
    {
        try
        {
            if (Environment. MachineName.Length != -1)
                throw new Exception("ThrewIt.");

            exOut = null;
            return true;
        }
        catch (Exception ex)
        {
            exOut = ex;
            return false;
        }
    }

3) 启动调试器

预期结果是 TestIgnoreException() 静默运行并返回 false.

Expected result is that TestIgnoreException() runs silently and returns false.

实际结果是调试器在 TestIgnoreException() 中停止,即使在该范围内不应该处理异常.

Actual result is the debugger stops in TestIgnoreException() even though there should be no exception being processed at that scope.

4) 也重新尝试使用 [DebuggerHidden] 代替,结果相同.

4) Also re-tried using [DebuggerHidden] instead, same results.

动机:

动机是针对某些不受您控制的 API 不提供Try"方法而仅通过使用异常来指示失败的情况.

The motivation is for cases where some API that is out of your control does not provide a "Try" method and instead only indicates failure by using exceptions.

众多此类示例之一是 .NET TcpClient.Connect(host, port).假设一个程序在启动期间总是测试一些连接,调试器不应该每次都停在这个特定的代码段上.

One of numerous such examples is .NET TcpClient.Connect(host, port). Say a program always tests some connections during startup, the debugger should not stop on this particular section of code each time.

使用标准的抛出时中断"异常复选框并不好,因为它按类型在全局范围内工作.它不能配置为在本地工作.其他检查代码的开发人员也应该自动跳过异常.

Using the standard "break when thrown" exceptions checkboxes is is not good because it works globally by type. It cannot be configured to work locally. Also other developers who check out the code should automatically skip the exception as well.

推荐答案

谜团解开.由于添加了异常处理优化,这确实是 MSVS 2015 的新已知问题.

Mystery solved. It is indeed a known issue that is new to MSVS 2015 because of added exception handling optimizations.

https://blogs.msdn.microsoft.com/visualstudioalm/2016/02/12/using-the-debuggernonusercode-attribute-in-visual-studio-2015/#

该链接上发布了一种解决方法,可以禁用优化并启用旧行为.希望他们最终能够恢复对此的支持,包括优化.

There is a workaround posted on that link to disable the optimizations and enable the old behavior. Hopefully they will eventually be able to revive the support for this including the optimizations.

reg add HKCUSoftwareMicrosoftVisualStudio14.0_ConfigDebuggerEngine /v AlwaysEnableExceptionCallbacksOutsideMyCode /t REG_DWORD /d 1

相关问题:

不要在抛出和捕获异常时停止调试器

这篇关于如何使 [DebuggerNonUserCode] 在简单的测试用例中对调试器隐藏异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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