C# Visual Studio 2008-发生未处理的异常时应用程序关闭

C# Visual Studio 2008-发生未处理的异常时应用程序关闭,c#,vb.net,visual-studio-2008,debugging,C#,Vb.net,Visual Studio 2008,Debugging,调试WinForm VB.NET项目时发生未绑定异常时,我遇到了一个问题 问题是我的应用程序终止,我必须重新启动应用程序,而不是像VS2003那样重试操作 未绑定异常在ApplicationEvents.vb中找到的新My.MyApplication类中实现 Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationService

调试WinForm VB.NET项目时发生未绑定异常时,我遇到了一个问题

问题是我的应用程序终止,我必须重新启动应用程序,而不是像VS2003那样重试操作

未绑定异常在ApplicationEvents.vb中找到的新My.MyApplication类中实现

Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
    Dim handler As New GlobalErrorHandler()
    handler.HandleError(e.Exception)

    e.ExitApplication = False
End Sub
注意:handler.HandleError只显示一个对话框,并将错误记录到日志文件中

我还尝试了在VS2003中使用的以下代码,但在VS2008中运行时会产生相同的行为:

    AddHandler System.Windows.Forms.Application.ThreadException, AddressOf OnApplicationErrorHandler
    AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf OnUnhandledExceptionHandler
OnApplicationOneErrorHandler和OnUnhandledExceptionHandler执行与handle.HandleError相同的操作

在VS2008之外运行应用程序会导致预期的行为(应用程序不会终止),但这会增加调试期间的测试周期


更新:我在回答中添加了示例代码,以在C#

中演示此问题。我不确定VS2008,但我在VS2005中遇到了同样的问题。事实证明,我只需要转到Debug->Exceptions(或Crtl+Alt+E)并确保所有抛出的框都未选中,但所有用户未处理的框都已选中


您的代码可能会有一些不同的、时髦的地方,但它可能会修复此行为。

好的,我在这方面做了更多的工作,但仍然没有答案。 我发现这个问题不仅与VB.NET有关,还与C有关#

下面是一个简单的C#程序,它演示了这个问题:

using System;
using System.Windows.Forms;
namespace TestCSharpUnhandledException
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show("Oops an unhandled exception, terminating:" + e.IsTerminating);                
        }

        void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            MessageBox.Show("Oops an unhandled thread exception");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            throw new Exception("Dummy unhandled exception");
        }
    }
}
我发现有趣的是在VS中调试UnhandledException事件被抛出,但在VS外部运行应用程序会引发ThreadException


同样在VS中,e.IsTerminating设置为true,最终我希望这是false。(这是一个只读属性)

好的,我在这篇博文中找到了这个问题的答案:

谢谢你,马克

简单的回答是:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
这会在调试和运行模式期间导致相同的行为。导致VS在调试时未关闭