即使在处理未经处理的异常后,应用程序也会崩溃并显示错误消息

本文关键字:处理 崩溃 显示 消息 错误 应用程序 异常 | 更新日期: 2023-09-27 18:34:15

我们订阅了未处理的线程异常和未处理的异常,如下所示

public partial class ICEView : Form
{
    public ICEView()
    {
        InitializeComponent();
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
        Application.ThreadException += new ThreadExceptionEventHandler(OnThreadException);
    }
}

有时,应用程序会在不输入异常处理程序的情况下崩溃,显示错误消息,如下面给出的链接所示。但是我们收到的错误消息没有"调试"按钮。显示消息框的原因可能是什么。

http://www.computerhope.com/issues/pictures/winerror.jpg

即使在处理未经处理的异常后,应用程序也会崩溃并显示错误消息

尝试在窗体之前添加 Program.cs 的 Main 方法中的异常处理程序,或者尝试在 InitializeComponent 方法之前添加异常处理程序。

[STAThread]
static void Main()
{
    Application.ThreadException += new ThreadExceptionEventHandler(OnThreadException);
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
    Application.Run(new ICEView());
}