欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# WinForm捕获未处理的异常实例解析

程序员文章站 2023-12-20 14:06:28
本文以一个完整的实例形式讲述了c# winform捕获未处理的异常的方法。分享给大家供大家参考之用。具体代码如下: using system; using sy...

本文以一个完整的实例形式讲述了c# winform捕获未处理的异常的方法。分享给大家供大家参考之用。具体代码如下:

using system;
using system.collections.generic;
using system.windows.forms;
using system.io;
namespace gobalexception
{
  static class program
  {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [stathread]
    static void main()
    {
      try
      {
        //处理未捕获的异常  
        application.setunhandledexceptionmode(unhandledexceptionmode.catchexception);
        //处理ui线程异常  
        application.threadexception += new system.threading.threadexceptioneventhandler(application_threadexception);
        //处理非ui线程异常  
        appdomain.currentdomain.unhandledexception += new unhandledexceptioneventhandler(currentdomain_unhandledexception);
        application.enablevisualstyles();
        application.setcompatibletextrenderingdefault(false);
        application.run(new form1());
      }
      catch (exception ex)
      {
        string str = "";
        string strdateinfo = "出现应用程序未处理的异常:" + datetime.now.tostring() + "\r\n";
        if (ex != null)
        {
          str = string.format(strdateinfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
             ex.gettype().name, ex.message, ex.stacktrace);
        }
        else
        {
          str = string.format("应用程序线程错误:{0}", ex);
        }

        writelog(str);
//frmbug f = new frmbug(str);//友好提示界面
      //f.showdialog();
        messagebox.show("发生致命错误,请及时联系作者!", "系统错误", messageboxbuttons.ok, messageboxicon.error);
      }
    }
    /// <summary>
    ///这就是我们要在发生未处理异常时处理的方法,我这是写出错详细信息到文本,如出错后弹出一个漂亮的出错提示窗体,给大家做个参考
    ///做法很多,可以是把出错详细信息记录到文本、数据库,发送出错邮件到作者信箱或出错后重新初始化等等
    ///这就是仁者见仁智者见智,大家自己做了。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    static void application_threadexception(object sender, system.threading.threadexceptioneventargs e)
    {
      
      string str = "";
      string strdateinfo = "出现应用程序未处理的异常:" + datetime.now.tostring() + "\r\n";
      exception error = e.exception as exception;
      if (error != null)
      {
        str = string.format(strdateinfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
           error.gettype().name, error.message, error.stacktrace);
      }
      else
      {
        str = string.format("应用程序线程错误:{0}", e);
      }
      writelog(str);  
//frmbug f = new frmbug(str);//友好提示界面
      //f.showdialog();
      messagebox.show("发生致命错误,请及时联系作者!", "系统错误", 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);
      }
      writelog(str);
//frmbug f = new frmbug(str);//友好提示界面
      //f.showdialog();
      messagebox.show("发生致命错误,请停止当前操作并及时联系作者!", "系统错误", messageboxbuttons.ok, messageboxicon.error);
    }
    /// <summary>
    /// 写文件
    /// </summary>
    /// <param name="str"></param>
    static void writelog(string str)
    {
      if (!directory.exists("errlog"))
      {
        directory.createdirectory("errlog");
      }
      using (streamwriter sw = new streamwriter(@"errlog\errlog.txt", true))
      {
        sw.writeline(str);
        sw.writeline("---------------------------------------------------------");
        sw.close();
      }
    }
  }
}

本文实例配有较为详尽的注释,便于大家阅读理解。希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: