在WinForm中使用全局异常捕获处理
  2fqsT8Yu1xAt 7天前 41 0

在WinForm中使用全局异常捕获处理

在WinForm应用程序中,全局异常捕获是确保程序稳定性的关键。通过在Program类的Main方法中设置全局异常处理,可以有效地捕获并处理未预见的异常,从而避免程序崩溃。

注册全局异常事件

[STAThread]
static void Main()
{
    // 注册全局异常捕获事件
    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    // 启动应用程序
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new MainForm());
}

处理未捕获的异常并记录日志

在全局异常事件处理方法中,我们可以编写逻辑来处理异常,例如记录日志、显示错误消息等。

// 处理未捕获的异常并记录日志
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    // 处理异常,如记录日志、显示错误消息等
    Exception exception = e.Exception;
    // ToDo: 处理异常的逻辑代码

    // 关闭应用程序或显示错误信息
    // Application.Exit();
    // MessageBox.Show("发生错误:" + exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

// 处理未捕获的异常并记录日志
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    // 处理异常,如记录日志、显示错误消息等
    Exception exception = e.ExceptionObject as Exception;
    // ToDo: 处理异常的逻辑代码

    // 关闭应用程序或显示错误信息
    // Application.Exit();
    // MessageBox.Show("发生错误:" + exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

示例代码

using System;
using System.Windows.Forms;
using exeStartTool;

namespace CopyTest
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        private static void Main()
        {
            //处理未捕获的异常
            //Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //ThreadException 处理UI线程异常
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            //UnhandledException 处理非UI线程异常
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new ExeStart());
        }

        static void Application_ThreadException(object sender,System.Threading.ThreadExceptionEventArgs e)
        {
            string str = "";
            string strDateInfo = "\r\n\r\n出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
            Exception error = e.Exception as Exception;
            if (error != null)
            {
                string logInfo = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",error.GetType().Name,error.Message,error.StackTrace);
                str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n",
                error.GetType().Name,error.Message);
            } else
            {
                str = string.Format("应用程序线程错误:{0}",e);
            }

            MessageBox.Show(str,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
        }

        static void CurrentDomain_UnhandledException(object sender,UnhandledExceptionEventArgs e)
        {
            string str = "";
            Exception error = e.ExceptionObject as Exception;
            string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
            if (error != null)
            {
                string logInfo = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆栈信息:{1}",error.Message,error.StackTrace);
                str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r",error.Message);
            } else
            {
                str = string.Format("Application UnhandledError:{0}",e);
            }

            MessageBox.Show(str,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
        }

    }
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 7天前 0

暂无评论

推荐阅读
  2fqsT8Yu1xAt   7天前   36   0   0 .NET
2fqsT8Yu1xAt