博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【改进】C# WinForm捕获全局异常 SamWang
阅读量:7209 次
发布时间:2019-06-29

本文共 5964 字,大约阅读时间需要 19 分钟。

  许多小公司的项目都缺少异常处理模块,我们也是。经常会出现这种情况,用户在UI界面操作,就直接跳出堆栈调用的异常信息对话框,老板看到那叫一个火啊!你们的代码怎么天天出现乱码。呵呵!这就是没有异常捕获处理导致的,现在许多人写代码都没意识处理异常,只要实现功能就好,我的许多组员也是如此。

  项目刚接手,所以打算做一个异常全局捕获,统一处理的模式,采用具体详细信息的对话框提醒与日志文件保存方式。以下是根据网上找的C#winform全局异常捕获做了点修改。(等项目异常处理全部完成后,将心得体会做个记录,此处暂对全局异常捕获做个记录)  

1     static class Program 2     { 3         ///  4         /// 应用程序的主入口点。 5         ///  6         [STAThread] 7         static void Main() 8         { 9             try10             {11                 //设置应用程序处理异常方式:ThreadException处理12                 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);13                 //处理UI线程异常14                 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);15                 //处理非UI线程异常16                 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);17 18                 #region 应用程序的主入口点19                 Application.EnableVisualStyles();20                 Application.SetCompatibleTextRenderingDefault(false);21                 Application.Run(new Form1());22                 #endregion23             }24             catch (Exception ex)25             {26                 string str = GetExceptionMsg(ex,string.Empty);27                 MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);28             }29         }30 31 32         static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)33         {34             string str = GetExceptionMsg(e.Exception, e.ToString());35             MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);36             //LogManager.WriteLog(str);37         }38 39         static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)40         {41             string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());42             MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);43             //LogManager.WriteLog(str);44         }45 46         /// 47         /// 生成自定义异常消息48         /// 49         /// 异常对象50         /// 备用异常消息:当ex为null时有效51         /// 
异常字符串文本
52 static string GetExceptionMsg(Exception ex,string backStr)53 {54 StringBuilder sb = new StringBuilder();55 sb.AppendLine("****************************异常文本****************************");56 sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());57 if (ex != null)58 { 59 sb.AppendLine("【异常类型】:" + ex.GetType().Name);60 sb.AppendLine("【异常信息】:" + ex.Message);61 sb.AppendLine("【堆栈调用】:" + ex.StackTrace);62 }63 else64 {65 sb.AppendLine("【未处理异常】:" + backStr);66 }67 sb.AppendLine("***************************************************************");68 return sb.ToString();69 }70 }

 

参考:

原代码
1 static class Program 2 { 3     ///  4     /// 应用程序的主入口点。 5     ///  6     [STAThread] 7     static void Main() 8     { 9         try10         {11             //处理未捕获的异常12             Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);13             //处理UI线程异常14             Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);15             //处理非UI线程异常16             AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);17 18             #region 应用程序的主入口点19 20             Application.EnableVisualStyles();21             Application.SetCompatibleTextRenderingDefault(false);22             Application.Run(new Main());23 24             #endregion25 26         }27         catch (Exception ex)28         {29             string str = "";30             string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";31 32             if (ex != null)33             {34                 str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",35                 ex.GetType().Name, ex.Message, ex.StackTrace);36             }37             else38             {39                 str = string.Format("应用程序线程错误:{0}", ex);40             }41 42             //MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);43             LogManager.WriteLog(str); 44         }45 46     }47 48 49     static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)50     {51         string str = "";52         string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";53         Exception error = e.Exception as Exception;54         if (error != null)55         {56             str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",57             error.GetType().Name, error.Message, error.StackTrace);58         }59         else60         {61             str = string.Format("应用程序线程错误:{0}", e);62         }63 64         //MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);65         LogManager.WriteLog(str);66     }67 68     static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)69     {70         string str = "";71         Exception error = e.ExceptionObject as Exception;72         string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";73         if (error != null)74         {75             str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆栈信息:{1}", error.Message, error.StackTrace);76         }77         else78         {79             str = string.Format("Application UnhandledError:{0}", e);80         }81 82         //MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);83         LogManager.WriteLog(str);84     }85 }

 

转载地址:http://derum.baihongyu.com/

你可能感兴趣的文章
Castle IOC容器实践之Startable Facility(二)
查看>>
APP-V与RemotoAPP
查看>>
使用Exchange反垃圾邮件组件解决内部仿冒邮件
查看>>
.Net Micro Framework研究—带IO的模拟器
查看>>
双ISP接入方案设计
查看>>
avascript解汉诺塔问题
查看>>
三种快速以太网标准
查看>>
Waymo乘客交互系统亮相,还带西方记者试乘了没司机的真·无人车
查看>>
IPv4和IPv6共存技术---ISATAP隧道
查看>>
【Cocoa(mac) Application 开发系列之二】总结一些常用控件及自定义View
查看>>
爪哇国新游记之十七----肺腑之言
查看>>
美国诚实签经验——必带材料:护照,证件照,DS160确认页,面试预约确认页,+境外照片...
查看>>
PHP乱码问题,UTF-8(乱码) (share)
查看>>
python 反模式
查看>>
interface
查看>>
学着使用移动应用统计分析工具
查看>>
ArcEngine中使用上下左右键移动地图
查看>>
discuz 帖子模块用到的表及自动发帖函数
查看>>
四、 用axis2的辅助工具发布、调用WebService
查看>>
关于字符串实现交叉合并字符串
查看>>