網站首頁 編程語言 正文
一個簡單的統一異常處理方法。系統底層出現異常,寫入記錄文件,系統頂層捕獲底層異常,顯示提示信息。?
/// <summary> /// 自定義異常類 /// </summary> public static class ExceptionExtension { /// <summary> /// 用戶自定義錯誤消息 /// </summary> public static string ErrorMessage { get; set; } /// <summary> /// 寫入異常日志 /// </summary> /// <param name="ex"></param> /// <param name="Message">用戶自定義錯誤消息</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($"*******異常發生時間:{DateTime.Now}*******\n"); errorInfo.AppendFormat(" 異常類型: {0} \n", ex.HResult); //msg.AppendFormat(" 導致當前異常的 Exception 實例: {0} \n", ex.InnerException); errorInfo.AppendFormat(" 導致異常的應用程序或對象的名稱: {0} \n", ex.Source); errorInfo.AppendFormat(" 引發異常的方法: {0} \n", ex.TargetSite); errorInfo.AppendFormat(" 異常堆棧信息: {0} \n", ex.StackTrace); errorInfo.AppendFormat(" 異常消息: {0} \n", ex.Message); errorInfo.AppendFormat(" 系統信息: {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();//將緩沖區的數據強制輸出,清空緩沖區 tw.Close();//關閉數據流 tw = null; } } catch (Exception) { Console.ReadKey(); } } } }
比較簡單,該類僅定義了一個屬性和一個方法。具體使用如下:系統底層(例如數據訪問層或業務邏輯層)發現異常時, 記錄異常信息,將異常上拋。例如:
//后臺處理 try { //有可能發生異常操作 } catch (Exception ex) { string strSlq = ""; ExceptionExtension.WriterExceptionLog(ex, "在查詢記錄時發生異常。SQL語句為:" + strSlq); throw;//向上拋出異常 } finally { //清理 }
用戶交互層,捕獲底層異常,顯示提示信息。例如:
//用戶界面 try { //調用底層有可能發生異常操作 } catch (Exception) { //MessageBox.Show(ExceptionExtension.ErrorMessage, "系統異常錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { //清理 }
原文鏈接:https://www.cnblogs.com/LiYunQi/archive/2021/12/31/15751493.html
相關推薦
- 2023-06-16 Dubbo?系列JDK?SPI?原理解析_服務器其它
- 2023-03-16 Python?asyncio異步編程簡單實現示例_python
- 2023-03-04 Qt利用tablewidget模擬手指實現滑動_C 語言
- 2022-07-03 go實現分布式鎖
- 2023-02-27 Python?input()函數案例教程_python
- 2022-07-17 baselines示例程序train_cartpole.py的ImportError_python
- 2022-07-26 arduino上傳程序出錯不成功常見的問題解決
- 2023-04-04 iOS數據持久化UserDefaults封裝器使用詳解_IOS
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支