当调试一个控制台应用程序时,Visual Studio陷在一个异常报告循环。为什么? [英] When debugging a console app, Visual Studio gets stuck in an exception-reporting loop. Why?

查看:119
本文介绍了当调试一个控制台应用程序时,Visual Studio陷在一个异常报告循环。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个简单的控制台应用程序:

Consider this simple console application:

using System;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            throw new Exception();
        }
    }
}



我在调试器下运行这个无论是在Visual Studio 2010中或Visual Studio 2012 Beta版。

I run this under the debugger in either Visual Studio 2010 or Visual Studio 2012 Beta.

当我这样做,自然调试器停在例外。好的,到目前为止

When I do so, naturally the debugger stops at the exception. Ok so far.

但是,当我按F5继续(或选择调试|继续),它在相同的异常再次停止。
我必须停止调试程序退出。我希望程序退出时,我按下F5。

But when I press F5 to continue (or choose Debug|Continue) it stops at the same exception again. I have to stop debugging for the program to exit. I expected the program to exit when I pressed F5.

有谁知道为什么它的行为,它呢?

Does anyone know why it behaves the way that it does?

我标志着一个答复,一个答案,但看到调试器的行为的一个奇怪的结果,考虑下​​面的代码:

I've marked a reply as an answer, but to see a weird consequence of the debugger's behaviour, consider the following code:

using System;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            throw new Exception();
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Console.WriteLine("Unhandled Exception.");
        }
    }
}



在调试器下运行这一点,按F5倍的负载,然后看输出。你会看到很多未处理的异常的消息,尽管该代码实际上只把它扔一次。调试器是导致抛出异常多次!这是我觉得很奇怪。

Run this under the debugger and press F5 a load of times, then look at the output. You'll see a lot of "Unhandled Exception" messages, despite the code only actually throwing it once. The debugger is causing the exception to be thrown multiple times! This is what I find strange.

推荐答案

有什么办法?

考虑下面的方法:

private void Test()
{
    throw new Exception();  
    int u = 4;
}

当异常被抛出,调试器允许您导航到调用上下文看看你的程序捕获该异常。如果不是这种情况,它从来没有跳过异常退出测试的方法,这就是为什么 INT U = 4; 。不可达

When the exception is thrown, the debugger allows you to navigate to the calling context to see if your program catches the exception. If it's not the case, it never exits the Test method by skipping the exception, that's why int u = 4; is unreachable.

在你的榜样,这是相同的:

In your example, it's the same:

private static void Main(string[] args)
{
    throw new Exception();

    // If I'm here, I will exit the application !
    // But this place is unreachable
}

您不能退出因为你的异常的方法范围。这就是为什么在使用F5调试你不能离开你的应用程序。

You can't exit the Main method scope because of your exception. That's why you can't exit your application while debugging using F5.

如果你没有调试器连接,当然崩溃您的应用程序,因为未处理的异常,但是这是另一回事。

If you have no debugger attached, your application will of course crash because of an unhandled exception, but this is another story.

这篇关于当调试一个控制台应用程序时,Visual Studio陷在一个异常报告循环。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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