Loading

Winforms 全局异常处理

很简单直接上代码

static class Program
{
    /// <summary>
    ///  The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        //设置应用程序处理异常方式:ThreadException处理
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        //处理UI线程异常
        Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
        //处理非UI线程异常
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

        // To customize application configuration such as set high DPI settings or default font,
        // see https://aka.ms/applicationconfiguration.
        ApplicationConfiguration.Initialize();
        Application.Run(new Form());
    }
    static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        MessageBox.Show("UI错误");
    }
    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        MessageBox.Show("非UI错误");
    }
}
posted @ 2024-03-29 08:43  重庆熊猫  阅读(13)  评论(0编辑  收藏  举报