为什么在调试UnhandledException事件时Visual Studio循环 [英] Why is Visual Studio looping when debugging UnhandledException events

查看:100
本文介绍了为什么在调试UnhandledException事件时Visual Studio循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这看起来与来自另一个线程的C#UnhandledException不断循环,但我不想在这里捕获异常,只是有机会记录一些东西)

(This looks very similar to C# UnhandledException from another thread keeps looping, but am not trying to catch the exception here, just get the chance to log something)

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

I have some very simple C# code that sets up an UnhandledException event handler, then throws an exception:

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");
    }
}

它的行为与我在控制台中所期望的一样:

It behaves as I expect from the console:

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中调试它时,它会进入一个循环,进入事件处理程序,然后退出以重新抛出异常。

But when I attempt to debug it in Visual Studio it enters a loop, going into the event handler then dropping out to rethrow the Exception.

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

The same thing happens when I express the handler as a distinct static method.

有什么想法吗?

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

This is in Visual Studio 2010. and .NET 4.

推荐答案

这似乎与<具体是a href = http://msdn.microsoft.com/en-us/library/ky9bw27e%28v=vs.100%29.aspx> ExceptionAssistant 。当您继续时,助手会将调用堆栈平移到引发异常的位置,这将导致异常被重新抛出。我认为这是为了允许您进行更改,从而避免发生异常。

It seems to be down to the behaviour of the ExceptionAssistant specfically. When you continue, the assistant unwinds the call stack to the point at which the exception was thrown -- which causes the exception to be rethrown. I assume this is to allow you to make changes that would allow you to avoid the exception.

如果在Tools\Options\Debugger\General下,取消选中 Unwind调用堆栈上未处理的异常,那么它将像独立进程一样运行,您将看到该进程终止。

If under Tools\Options\Debugger\General you uncheck "Unwind the call stack on unhandled exceptions" then it'll just behave as an independent process would behave, and you'll see the process terminate.

这篇关于为什么在调试UnhandledException事件时Visual Studio循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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