zoukankan      html  css  js  c++  java
  • WinForm全局异常捕获

        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                try
                {
                    //设置应用程序处理异常方式:ThreadException处理
                    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                    //处理UI线程异常
                    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                    //处理非UI线程异常
                    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    
                    #region 应用程序的主入口点
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                    #endregion
                }
                catch (Exception ex)
                {
                    string str = GetExceptionMsg(ex,string.Empty);
                    MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    
    
            static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
            {
                string str = GetExceptionMsg(e.Exception, e.ToString());
                MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //LogManager.WriteLog(str);
            }
    
            static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
                MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //LogManager.WriteLog(str);
            }
    
            /// <summary>
            /// 生成自定义异常消息
            ///  这就是我们要在发生未处理异常时处理的方法,我这是写出错详细信息到文本,如出错后弹出一个漂亮的出错提示窗体,给大家做个参考
            ///  做法很多,可以是把出错详细信息记录到文本、数据库,发送出错邮件到作者信箱或出错后重新初始化等等
            ///  这就是仁者见仁智者见智,大家自己做了。
            /// </summary>
            /// <param name="ex">异常对象</param>
            /// <param name="backStr">备用异常消息:当ex为null时有效</param>
            /// <returns>异常字符串文本</returns>
            static string GetExceptionMsg(Exception ex,string backStr)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("****************************异常文本****************************");
                sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());
                if (ex != null)
                {
                    sb.AppendLine("【异常类型】:" + ex.GetType().Name);
                    sb.AppendLine("【异常信息】:" + ex.Message);
                    sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
                }
                else
                {
                    sb.AppendLine("【未处理异常】:" + backStr);
                }
                sb.AppendLine("***************************************************************");
                return sb.ToString();
            }
        }

    转载自:https://www.cnblogs.com/wangshenhe/archive/2012/11/14/2769605.html

  • 相关阅读:
    atitit.按钮光标滑过高亮切换以及其他动态效果的实现css html js attilax总结
    atitit. 文件上传带进度条 atiUP 设计 java c# php
    atitit.新增编辑功能 跟orm的实现 attilax p31
    atitit. java jsoup html table的读取解析 总结
    atitit.设计文档操作日志的实现
    atitit.资源释放机制attilax总结
    (转)Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
    (转)Android笔记handler机制
    (转)数据存储
    (转)android连网详解
  • 原文地址:https://www.cnblogs.com/JourneyOfFlower/p/13748281.html
Copyright © 2011-2022 走看看