【问题标题】:Why is Visual Studio looping when debugging UnhandledException events为什么调试 UnhandledException 事件时 Visual Studio 循环
【发布时间】:2013-12-29 07:44:01
【问题描述】:

(这看起来与C# UnhandledException from another thread keeps looping 非常相似,但我不想在这里捕获异常,只是有机会记录一些东西)

我有一些非常简单的 C# 代码,用于设置 UnhandledException 事件处理程序,然后引发异常:

class Program
{
    static void Main(string[] args)
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        //currentDomain.UnhandledException += new UnhandledExceptionEventHandler(currentDomain_UnhandledException);

        currentDomain.UnhandledException += (sender, eventArgs) =>
            {
                var exception = (Exception) eventArgs.ExceptionObject;

                Console.WriteLine("Unhandled exception: " + exception.Message);
            };

        throw new AccessViolationException("Bleurgh");
    }
}

它的行为符合我对控制台的期望:

Unhandled exception: Bleurgh
Unhandled Exception: System.AccessViolationException: Bleurgh
    at UnhandledExceptions.Program.Main(String[] args) in c:\code\sandbox\UnhandledExceptions\UnhandledExceptions\Program.cs:line 20

但是当我尝试在 Visual Studio 中调试它时,它会进入一个循环,进入事件处理程序,然后退出以重新引发异常。

当我将处理程序表示为不同的静态方法时,也会发生同样的事情。

有什么想法吗?

这是在 Visual Studio 2010 中。编辑:和 .NET 4。

【问题讨论】:

标签: c# visual-studio-2010 c#-4.0 unhandled-exception


【解决方案1】:

这似乎具体取决于ExceptionAssistant 的行为。当您继续时,助手将调用堆栈展开到引发异常的点——这会导致再次引发异常。我认为这是为了让您可以进行更改以避免异常。

如果在 Tools\Options\Debugger\General 下取消选中“Unwind the call stack on unhandled exceptions”,那么它的行为就像一个独立的进程一样,你会看到进程终止。

【讨论】:

  • 最后,我在去年左右寻找的答案!到目前为止,我认为不可能以正确的方式让它失败。
【解决方案2】:

这就是调试器的工作方式,或者我们应该说,一个“功能”。如果进程是由调试器 (F5) 创建的,则调试器将阻止进程终止,并将您指向将导致进程终止的代码行。那些“未处理的异常”实际上是由调试器处理的,因此执行永远不会到达您的代码。

如果在进程创建后将调试器附加到进程(Ctrl+F5,然后附加),那么调试器最终会到达未处理的异常“处理程序”,但在退出处理程序后它仍然会阻止进程终止,并带来你回到发生异常的地方。

【讨论】:

  • 有趣;如果我删除了我的 UnhandledException 事件处理程序,那么调试器也只是循环,再次将行指针重置为 throw。
  • @TimBarrass 我更正了答案。在调试器下,进程简单地变成“不可终止”。
  • 感谢 Diaicticus -- 我认为这很有帮助,但不是完整的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2016-04-21
  • 2010-09-24
相关资源
最近更新 更多