VS2022 - 抛出异常时无法查看变量的值 - 局部变量和参数在“[Exception]”调用堆栈帧中不可用

问题描述 投票:0回答:3

我抛出了 InvalidOperationException(这应该很容易修复)。但是,当引发异常时,我无法在本地调试窗口中查看任何当前值。他们都有相同的信息:

Local variables and arguments are not available in '[Exception]' call stack frames. To obtain these, configure the debugger to stop when the exception is thrown and rerun the scenario.

不确定为什么会发生这种情况(也许与异步有关?),但这是相对简单的代码,我应该能够看到这些变量的值。

c# exception visual-studio-debugging visual-studio-2022
3个回答
19
投票

您可以进入“调试”>“Windows”>“异常设置”并选择“System.InvalidOperationException”来解决该问题。


1
投票

经过一段时间的搜索,我也偶然发现了你的问题。

是的,接受的答案是解决您问题的方法 -> 您可以转到“调试”>“Windows”>“异常设置”并选择“System.InvalidOperationException”,如上面答案中所述。

但是,这将始终中断任何“System.InvalidOperationException”,即使异常是在 try 和 catch 块中处理的。

或者,我建议您打开“调用堆栈窗口”(调试 > 窗口 > 调用堆栈)。

在这里(不确定这是否是 Visual Studio 2022 中的新功能,因为在 Visual Studio 2019 中我没有这个问题)您将看到两个几乎相同的调用堆栈:

  • 以 [Exception] 开头,例如“[Exception] ...调用堆栈数据...”
  • 另一个相同但没有[异常],例如“...调用堆栈数据...”

如果双击第二个变量,您的局部变量将被填充 - 而在第一个带有 [Exception] 的变量中,您的局部变量不会被填充(这是发生异常时的默认变量。

您的错误消息中也间接描述了此行为:

局部变量和参数在“[Exception]”调用堆栈帧中不可用。

不幸的是,我不知道为什么 Visual Studio 2022 现在会在发生异常时创建两个类似的调用堆栈,而 Visual Studio 2019 却不会。


0
投票

在 .NET 6 应用程序上的 Visual Studio 2022 中,当调试器在异常重新引发期间停止执行时,我注意到此行为:

try
{
     // some code that throws an exception
}
catch (Exception e)
{
     // some custom exception handling
     throw; // *** This is where the debugger is stopped ***
}

我的解决方案可能并不适用于所有情况,是使用预处理器指令在调试期间不捕获并重新抛出异常:

#if DEBUG
#else
    try
    {
#end
         // some code that throws an exception
         // *** debugger now stops here, and locals/watch expressions work as expected ***
#if DEBUG
#else
    }
    catch (Exception e)
    {
        // some custom exception handling
        throw;
    }
#end
© www.soinside.com 2019 - 2024. All rights reserved.