代码之家  ›  专栏  ›  技术社区  ›  Philip Fourie

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

  •  2
  • Philip Fourie  · 技术社区  · 17 年前

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

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

    unndeld异常是在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
    

    注:处理程序。HandleError只显示一个对话框,并将错误记录到日志文件中。

    我还尝试了以下代码,这些代码以前在VS2003中工作,但在VS2008中运行时会产生相同的行为:

        AddHandler System.Windows.Forms.Application.ThreadException, AddressOf OnApplicationErrorHandler
        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf OnUnhandledExceptionHandler
    

    OnApplicationErrorHandler和OnUnhandleedExceptionHandler的作用与句柄相同。处理错误

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

    更新: 我在答案中添加了示例代码,用C演示了这个问题#

    3 回复  |  直到 17 年前
        1
  •  1
  •   Philip Fourie    17 年前

    好吧,我在这方面做了更多的工作,但仍然没有答案。 我发现这个问题不仅与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未处理的异常 事件被抛出,但在VS外部运行应用程序会导致 线程异常 开火。

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

        2
  •  1
  •   Philip Fourie    17 年前

    好的,我在这篇博客文章中找到了这个问题的答案: Handling "Unhandled Exceptions" in .NET 2.0

    谢谢你,马克!

    简短的回答是:

    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    

    这会导致在调试和运行模式下出现相同的行为。导致VS在调试时未关闭

        3
  •  0
  •   Jeffrey    17 年前

    我不确定VS2008,但我在VS2005有一段时间遇到了同样的问题。原来我只需要去调试->;异常(或Crtl+Alt+E),并确保所有“抛出”框均未选中,但所有“用户未处理”框均已选中。

    你的代码可能有一些不同和时髦的地方,但它可能会修复这种行为。