日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# winform 捕获全局异常

發(fā)布時間:2025/7/25 C# 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# winform 捕获全局异常 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

namespace GobalException
{
??? static class Program
??? {
??????? /// <summary>
??????? /// 應(yīng)用程序的主入口點。
??????? /// </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 = "出現(xiàn)應(yīng)用程序未處理的異常:" + 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("應(yīng)用程序線程錯誤:{0}", ex);
??????????????? }


??????????????? writeLog(str);
??????????????? MessageBox.Show("發(fā)生致命錯誤,請及時聯(lián)系作者!", "系統(tǒng)錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
??????????? }

??????? }

??????? /// <summary>
??????? ///這就是我們要在發(fā)生未處理異常時處理的方法,我這是寫出錯詳細(xì)信息到文本,如出錯后彈出一個漂亮的出錯提示窗體,給大家做個參考
??????? ///做法很多,可以是把出錯詳細(xì)信息記錄到文本、數(shù)據(jù)庫,發(fā)送出錯郵件到作者信箱或出錯后重新初始化等等
??????? ///這就是仁者見仁智者見智,大家自己做了。
??????? /// </summary>
??????? /// <param name="sender"></param>
??????? /// <param name="e"></param>
??????? static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
??????? {
???????????
??????????? string str = "";
??????????? string strDateInfo = "出現(xiàn)應(yīng)用程序未處理的異常:" + 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("應(yīng)用程序線程錯誤:{0}", e);
??????????? }

??????????? writeLog(str);???
??????????? MessageBox.Show("發(fā)生致命錯誤,請及時聯(lián)系作者!", "系統(tǒng)錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
??????? }

??????? static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
??????? {
??????????? string str = "";
??????????? Exception error = e.ExceptionObject as Exception;
??????????? string strDateInfo = "出現(xiàn)應(yīng)用程序未處理的異常:" + 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);
??????????? MessageBox.Show("發(fā)生致命錯誤,請停止當(dāng)前操作并及時聯(lián)系作者!", "系統(tǒng)錯誤", 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();
??????????? }
??????? }
??? }
}

轉(zhuǎn)載于:https://www.cnblogs.com/kevinGao/archive/2011/11/02/2233420.html

總結(jié)

以上是生活随笔為你收集整理的C# winform 捕获全局异常的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。