调试UnhandledException事件时,为什么Visual Studio会循环

Tim*_*ass 8 c# visual-studio-2010 unhandled-exception c#-4.0

(这看起来非常类似于来自另一个线程的C#UnhandledException保持循环,但我不是试图在这里捕获异常,只是有机会记录一些东西)

我有一些非常简单的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");
    }
}
Run Code Online (Sandbox Code Playgroud)

它的行为与我期望从控制台一样:

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

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

当我将处理程序表示为一个独特的静态方法时,会发生同样的事情.

有什么想法发生了什么?

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

Tim*_*ass 11

它似乎取决于ExceptionAssistant的行为.当您继续时,助手会将调用堆栈展开到抛出异常的位置 - 这会导致异常被重新抛出.我假设这是允许您进行更改,以避免异常.

如果在Tools\Options\Debugger\General下取消选中"在未处理的异常上展开调用堆栈",那么它只会表现为独立进程的行为,并且您将看到进程终止.

  • 最后,我在寻找去年左右的答案!到目前为止,我认为不可能以正确的方式失败. (4认同)