網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一個(gè)簡(jiǎn)單的統(tǒng)一異常處理方法。系統(tǒng)底層出現(xiàn)異常,寫(xiě)入記錄文件,系統(tǒng)頂層捕獲底層異常,顯示提示信息。?
/// <summary> /// 自定義異常類 /// </summary> public static class ExceptionExtension { /// <summary> /// 用戶自定義錯(cuò)誤消息 /// </summary> public static string ErrorMessage { get; set; } /// <summary> /// 寫(xiě)入異常日志 /// </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 { //清理 }
原文鏈接:https://www.cnblogs.com/LiYunQi/archive/2021/12/31/15751493.html
相關(guān)推薦
- 2022-06-22 C++深入刨析muduo中的抽象類Poller_C 語(yǔ)言
- 2022-07-11 MongoDB分片方式及片鍵選擇
- 2024-02-26 JqGrid獲得所有選中行數(shù)據(jù)ID數(shù)組,獲取所有行的ID數(shù)組
- 2022-06-11 Python?tkinter庫(kù)圖形繪制例子分享_python
- 2023-02-14 教你使用SQL語(yǔ)句進(jìn)行數(shù)據(jù)庫(kù)復(fù)雜查詢_MsSql
- 2022-10-30 Matlab利用遺傳算法GA求解非連續(xù)函數(shù)問(wèn)題詳解_C 語(yǔ)言
- 2022-07-02 如何對(duì)numpy?矩陣進(jìn)行通道間求均值_python
- 2022-10-31 關(guān)于命令行執(zhí)行Python腳本的傳參方式_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支