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

问题描述

设置:



1)MSVS 2015,选项 - >调试器 - >Just My Code

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

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

[DebuggerNonUserCode] //防止异常在此方法中停止调试器。
static bool TrySomething(out Exception exOut)
{
try
{
if(Environment。MachineName.Length!= -1)
throw new Exception ( ThrewIt。);

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

3)启动调试器



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



实际结果是TestIgnoreException()中的调试器停止,即使应该在该范围内处理的例外情况也不例外。





4)也可以使用[DebuggerHidden]重新尝试相同的结果。



动机:



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



许多这样的示例之一是.NET TcpClient.Connect(主机,端口)。说一个程序总是在启动期间测试一些连接,每次调试器不应该停在这个特定的代码段上。



使用标准的break throw throw异常复选框不是很好,因为它通过类型全局工作。它不能配置为在本地工作。还有其他开发人员也可以自动跳过异常。

解决方案

神秘解决。由于增加了异常处理优化,确实是一个已知的问题,因为MSVS 2015是新的。





该链接上有一个解决方法,用于禁用优化并启用旧的行为。希望他们最终能够恢复支持,包括优化。

  reg添加HKCU\Software\Microsoft\\ \\ VisualStudio\14.0_Config\Debugger\Engine / v AlwaysEnableExceptionCallbacksOutsideMyCode / t REG_DWORD / d 1 

相关问题:




Setup:

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

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) Launch Debugger

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

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

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

Motivation:

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.

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.

解决方案

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 HKCU\Software\Microsoft\VisualStudio\14.0_Config\Debugger\Engine /v AlwaysEnableExceptionCallbacksOutsideMyCode /t REG_DWORD /d 1

Related question:

Don't stop debugger at THAT exception when it's thrown and caught

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

11-01 16:19