欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# WINFORM自定義異常處理方法

 更新時(shí)間:2021年12月31日 08:39:41   作者:李運(yùn)琪  
這篇文章主要介紹了一個(gè)簡(jiǎn)單的統(tǒng)一異常處理方法。系統(tǒng)底層出現(xiàn)異常,寫入記錄文件,系統(tǒng)頂層捕獲底層異常,顯示提示信息。需要的可以參考一下

一個(gè)簡(jiǎn)單的統(tǒng)一異常處理方法。系統(tǒng)底層出現(xiàn)異常,寫入記錄文件,系統(tǒng)頂層捕獲底層異常,顯示提示信息。?

 /// <summary>
    /// 自定義異常類
    /// </summary>
    public static class ExceptionExtension
 
    {
        /// <summary>
        /// 用戶自定義錯(cuò)誤消息
        /// </summary>
        public static string ErrorMessage { get; set; }
 
        /// <summary>
        /// 寫入異常日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="Message">用戶自定義錯(cuò)誤消息</param>
        public static void WriterExceptionLog(Exception ex, string Message = "")
        {
            string filePath = Environment.CurrentDirectory.Replace(@"\bin\Debug", "") + @"\ErrorLog";
            if (!System.IO.Directory.Exists(filePath))
            {
                System.IO.Directory.CreateDirectory(filePath);
            }
            string fileName = filePath + @"\ErrorLog.txt";
 
            StringBuilder errorInfo = new StringBuilder();
            errorInfo.Append($"*******異常發(fā)生時(shí)間:{DateTime.Now}*******\n");
            errorInfo.AppendFormat(" 異常類型: {0} \n", ex.HResult);
            //msg.AppendFormat(" 導(dǎo)致當(dāng)前異常的 Exception 實(shí)例: {0} \n", ex.InnerException);
            errorInfo.AppendFormat(" 導(dǎo)致異常的應(yīng)用程序或?qū)ο蟮拿Q: {0} \n", ex.Source);
            errorInfo.AppendFormat(" 引發(fā)異常的方法: {0} \n", ex.TargetSite);
            errorInfo.AppendFormat(" 異常堆棧信息: {0} \n", ex.StackTrace);
            errorInfo.AppendFormat(" 異常消息: {0} \n", ex.Message);
            errorInfo.AppendFormat(" 系統(tǒng)信息: {0} \n", Message);
            ErrorMessage += Message;
            try
            {
                if (File.Exists(fileName))
                {
                    using (StreamWriter tw = File.AppendText(fileName))
                    {
                        tw.WriteLine(errorInfo.ToString());
                    }
                }
                else
                {
                    TextWriter tw = new StreamWriter(fileName);
                    tw.WriteLine(errorInfo.ToString());
                    tw.Flush();//將緩沖區(qū)的數(shù)據(jù)強(qiáng)制輸出,清空緩沖區(qū)
                    tw.Close();//關(guān)閉數(shù)據(jù)流
                    tw = null;
                }
            }
            catch (Exception) { Console.ReadKey(); }
        }
    }
}

比較簡(jiǎn)單,該類僅定義了一個(gè)屬性和一個(gè)方法。具體使用如下:系統(tǒng)底層(例如數(shù)據(jù)訪問(wèn)層或業(yè)務(wù)邏輯層)發(fā)現(xiàn)異常時(shí), 記錄異常信息,將異常上拋。例如:

//后臺(tái)處理
try
{
    //有可能發(fā)生異常操作
}
catch (Exception ex)
{
    string strSlq = "";
    ExceptionExtension.WriterExceptionLog(ex, "在查詢記錄時(shí)發(fā)生異常。SQL語(yǔ)句為:" + strSlq);
    throw;//向上拋出異常
}
finally
{
    //清理
}

用戶交互層,捕獲底層異常,顯示提示信息。例如:

//用戶界面
 try
 {
     //調(diào)用底層有可能發(fā)生異常操作
 }
 catch (Exception)
 {
     //MessageBox.Show(ExceptionExtension.ErrorMessage, "系統(tǒng)異常錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
 }
 finally
 {
     //清理
 }

到此這篇關(guān)于C# WINFORM自定義異常處理方法的文章就介紹到這了,更多相關(guān)C# WINFORM異常處理方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論