• WinForm全局逻辑未处理异常的异常处理


    static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                // 处理未捕获的异常
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                // 捕获程序中未处理的异常(UI线程异常)
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                // 捕获程序中未处理的异常(非UI线程异常)
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    
                Application.Run(new Form1());
            }
    
            /// <summary>
            /// 捕获程序中未处理的异常(UI线程异常)
            /// </summary>
            private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
            {
                Exception ex = e.Exception;
                string str = ex.StackTrace;
    
                //Log4NetHelper.WriteError(typeof(Exception), "UI线程异常;异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message);
                MessageBox.Show("软件致命报错:" + ex.Message, "软件致命异常");
            }
    
            /// <summary>
            /// 捕获程序中未处理的异常(非UI线程异常)
            /// </summary>
            private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                Exception ex = e.ExceptionObject as Exception;
                string str = ex.StackTrace;
                //Log4NetHelper.WriteError(typeof(Exception), "非UI线程异常;异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message);
                MessageBox.Show("软件致命报错:" + ex.Message, "软件致命异常");
            }
        }
  • 相关阅读:
    【六校联合训练 省选 #20】快递
    IOError: cannot open resource
    [已解决]运行gunicorn失败:[ERROR] Connection in use 127.0.0.1 8080
    windows下通过navicat for mysql连接centos6.3-64bit下的MySQL数据库
    在centos7中使用yum安装mysql数据库并使用navicat连接
    centos出现“FirewallD is not running”怎么办
    [linux]centos7下解决yum install mysql-server没有可用包
    CentOS 7安装Python3
    flask——CSRFToken保护
    python 获取当前文件夹下所有文件名
  • 原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/16832703.html
Copyright © 2020-2023  润新知