static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </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 FrmActivity());
}
catch (Exception ex)
{
string str = "";
string strDateInfo = "出现应用程序未处理的异常:" + 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("应用程序线程错误:{0}", ex);
} //写日志
WriteLog.WriteErrLog(str);
MessageBox.Show("发生致命错误,请及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
} } /// <summary>
///这就是我们要在发生未处理异常时处理的方法,做法很多,可以是把出错详细信息记录到文本、数据库,发送出错邮件到作者信箱或出错后重新初始化等等
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{ string str = "";
string strDateInfo = "出现应用程序未处理的异常:" + 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("应用程序线程错误:{0}", e);
}
//写日志
WriteLog.WriteErrLog(str);
MessageBox.Show("发生致命错误,请及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
/// <summary>
/// ' 处理UI异常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = "";
Exception error = e.ExceptionObject as Exception;
string strDateInfo = "出现应用程序未处理的异常:" + 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.WriteErrLog(str);
MessageBox.Show("发生致命错误,请停止当前操作并及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }

WinForm C#全局错误捕捉处理【整理】的更多相关文章

  1. Android 全局错误管理

    package com.wlwl.yiyuan; import java.io.File; import java.io.PrintWriter; import java.io.StringWrite ...

  2. 如何捕获winform程序全局异常?

    1.在C#中我们如何处理异常? 上面的问题学过C#的问题大家可能都能回答处理,用try-catch-finally具体如下: try { //可能出错的语句 } catch (Exception) { ...

  3. c#winform将全局异常抛出,不用大量写try()catch()

    一.在program.cs处完善成如下,但是这样后只能抛出主线程(UI)的错误,所以请看第二步 /// 应用程序的主入口点. /// </summary> [STAThread] stat ...

  4. android中全局异常捕捉

    android中全局异常捕捉 只要写代码就会有bug,但是我们要想办法收集到客户的bug.有第三方bugly或者友盟等可以收集.但是,android原生就提供了有关收集异常的api,所以我们来学习一下 ...

  5. asp.net 使用HttpModule记录全局错误

    以前使用Global.asax记录全局的错误日志觉得挺好用,但是如果一个解决方案下有N多个项目,每个下边都需要加一个并且代码都还是重复的,终于有一天无法再忍受这种模式,考虑到HttpModule,直接 ...

  6. 用VSCode开发一个asp.net core 2.0+angular 5项目(4): Angular5全局错误处理

    第一部分: http://www.cnblogs.com/cgzl/p/8478993.html 第二部分: http://www.cnblogs.com/cgzl/p/8481825.html 第三 ...

  7. [翻译]ASP.NET Web API 2 中的全局错误处理

    目录 已存在的选项 解决方案预览 设计原则 什么时候去用 方案详情 示例 附录: 基类详情 原文链接 Global Error Handling in ASP.NET Web API 2 由于翻译水平 ...

  8. Spring 全局异常捕捉

    Spring全局异常捕捉类 注解@ControllerAdvice package com.sicdt.sicsign.web.bill.controller; import org.springfr ...

  9. springboot(四)拦截器和全局异常捕捉

    github代码:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service 全部 ...

随机推荐

  1. inline(内联元素)和block(块级元素) 的区别

    块级元素,默认是独自占据一行的.比如是<p>.<h1>.<h2>.<h3>.<h4>.<h5>.<h6>.<u ...

  2. Python学习 之 文件

    1.文件读写 python进行文件读写的函数是open或file file_handler=open(filename,,mode) (1)打开并读取文件 方式一:open() fo=open('/r ...

  3. 使用asp.net动态添加html元素

    HtmlGenericControl gen = new HtmlGenericControl("div");        gen.InnerText = "HtmlG ...

  4. Linux 引导过程内幕

    转载:http://www.ibm.com/developerworks/cn/linux/l-linuxboot/index.html   从主引导记录到第一个用户空间应用程序的指导 引导 Linu ...

  5. eclipse按Crl+鼠标左键,找不到源文件的解决办法。

    这种情况一般发生在tomcat的之中,原因是缺少类的源文件.在jdk中很少见,jdk中自带类的源文件,配置jdk的时候就已经将其加载进来了.而tomcat之中没有带类的源文件,需要自己去网上单独下载. ...

  6. [PHP] Eclipse开发PHP环境配置

    首先准备好软件: 1. Apache,到这里找个最新版本 2. PHP,到这里下载 3. Eclipse IDE for Java EE Developers,到这里下载 4. DLTK Core F ...

  7. Org-mode 任务添加提醒

    org-mode中的约会日程有提醒功能,这样设置就行: ;; ;;; 提醒 ;; (require 'appt) ;; (appt-activate t);启用约会提醒 ;; (setq appt-d ...

  8. Android Studio 复制粘贴图片到drawable文件夹没有效果 - 解决方法

    我想放一些图片到drawable文件夹里面,但是简单的复制文件,粘贴文件,或者拖拽文件,都不起作用.不知道为什么,之前是可以的,突然就不行了. 解决方案 在drawable文件夹的目录上右键,选择Re ...

  9. [转]Asp.Net MVC 扩展联想控件

    本文转自:http://www.cnblogs.com/bright-lin/archive/2013/02/06/MVC_SuggestBox.html 在web中,为改善用户体验,我们常会将一些文 ...

  10. ORACLE临时表 转 学习用

    转:http://www.2cto.com/database/201210/163979.html 临时表:像普通表一样,有结构,但是对数据的管理上不一样,临时表存储事务或会话的中间结果集,临时表中保 ...