zoukankan      html  css  js  c++  java
  • WINFORM自动捕获异常

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
     
    namespace WinformLogDemo
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                try
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
     
     
                    Application.Run(new Form1());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "系统错误"MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
     
        
     
            static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
            {
                string str = "";
                Exception error = e.Exception as Exception;
                if (error != null)
                {
                    str = string.Format("异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n", error.GetType().Name, error.Message, error.StackTrace);
                }
                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)
                {
                    str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆栈信息:{1}", error.Message, error.StackTrace);
                }
                else
                {
                    str = string.Format("Application UnhandledError:{0}", e);
                }
     
                MessageBox.Show("发生致命错误,请停止当前操作并及时联系作者!""系统错误"MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
    
    参考资料:

    -------------------------------------------------------------------------------------------------------------------------------------------------
    数据库优化
    数据库教程
    数据库实战经验分享博客

    百度云下载

    评测


  • 相关阅读:
    461.mysql数据的备份和恢复
    thinkcmf安装遇到的问题【服务器rewrite】未开启
    navicat for mysql 中文破解版(无需激活码)
    centos7--docker安装gitlab时权限异常
    Cenos7 ++Zabbix监控(同个节点)
    Centos-第一次机子遭受黑客入侵??很鸡冻
    Linux运维--企业sudo权限规划详解 (实测一个堆命令搞定)
    Centos7--sudo的使用和配置
    网络技术--IPv4子网掩码的理解和计算。
    CentOS7--部署JSP网站项目(环境架构jsp+mysql+tomcat)
  • 原文地址:https://www.cnblogs.com/longle/p/2915812.html
Copyright © 2011-2022 走看看