C# WinForm捕获未处理的异常
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
namespace GobalException
{
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 Form1());
}
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(str);
//frmBug f = new frmBug(str);//友好提示界面
//f.ShowDialog();
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(str);
//frmBug f = new frmBug(str);//友好提示界面
//f.ShowDialog();
MessageBox.Show("发生致命错误,请及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
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(str);
//frmBug f = new frmBug(str);//友好提示界面
//f.ShowDialog();
MessageBox.Show("发生致命错误,请停止当前操作并及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
/// <summary>
/// 写文件
/// </summary>
/// <param name="str"></param>
static void writeLog(string str)
{
if (!Directory.Exists("ErrLog"))
{
Directory.CreateDirectory("ErrLog");
}
using (StreamWriter sw = new StreamWriter(@"ErrLog\ErrLog.txt", true))
{
sw.WriteLine(str);
sw.WriteLine("---------------------------------------------------------");
sw.Close();
}
}
}
}
C# WinForm捕获未处理的异常的更多相关文章
- WPF捕获未处理的异常
WPF程序中,对于异常的捕获一般使用try/catch块.就像程序中的bug一样,很难保证程序中所有的异常都能够通过try/catch捕获.如果异常没有被捕获,轻则影响用户体验,严重时会导致数据丢失 ...
- C# WinForm捕获全局异常(捕获未处理的异常)
static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static vo ...
- android 捕获未try的异常
1.Thread.UncaughtExceptionHandler java里有很多异常如:空指针异常,越界异常,数值转换异常,除0异常,数据库异常等等.如果自己没有try / catch 那么线程就 ...
- android 捕获未try的异常、抓取崩溃日志
1.Thread.UncaughtExceptionHandler java里有很多异常如:空指针异常,越界异常,数值转换异常,除0异常,数据库异常等等.如果自己没有try / catch 那么线程就 ...
- C# WINFORM 捕获全局异常
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Thr ...
- Android程序捕获未处理异常,处理与第三方方法冲突时的异常传递
自己的android程序对异常进行了处理,用的也是网上比较流行的CrashHandler,代码如下,就是出现了未处理的异常程序退出,并收集收集设备信息和错误信息仪器保存到SD卡,这里没有上传到服务器. ...
- Error:(12, 64) java: 未报告的异常错误java.io.IOException; 必须对其进行捕获或声明以便抛出
Error:(12, 64) java: 未报告的异常错误java.io.IOException; 必须对其进行捕获或声明以便抛出 package com.test; import org.apach ...
- c# winform捕获全局异常,并记录日志
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using ...
- 编写高质量代码改善C#程序的157个建议——建议66:正确捕获多线程中的异常
建议66:正确捕获多线程中的异常 多线程的异常处理需要采用特殊的方式.一下这种方式会存在问题: try { Thread t = new Thread((ThreadStart)delegate { ...
随机推荐
- DataContext 数据在F5刷新频繁,会出现数据读取错误
DataContext 数据在F5刷新频繁,会出现数据读取错误 DataContext是 Linq to sql数据模型的底层数据库对象所有LInq数据表对象都是由它派生的, 只要建立一个数据库操作, ...
- Java编程风格与命名规范整理
基本命名规范 包命名 包名按照域名的范围从大到小逐步列出,恰好和Internet上的域名命名规则相反. 由一组以“.”连接的标识符构成,通常第一个标识符为符合网络域名的两个或者三个英文小写字母. Pe ...
- swift 自行理解
- Net中exe之间的消息传递
1.创建一个消息通讯类 using System;using System.Collections.Generic;using System.Linq;using System.Text;using ...
- DbUtil组件及C3P0数据库连接池组件的使用
DbUtils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性能. 使用c ...
- Windwos平台上ffmpeg解码音频并且保存到wav文件中
先附上代码,测试通过 #include <stdio.h> #include <math.h> #include "libavutil/avstring.h" ...
- Qt 之 QtScript
前言 前面学习中,很多地方都用到了C++和JavaScript相互通信.今天就学习QtScript模块吧. Qt 包含完全集成的 ECMA 标准脚本引擎.Qt Script 集成了 QObject,为 ...
- 九度OJ 1107 搬水果 -- 哈夫曼树 2011年吉林大学计算机研究生机试真题
题目地址:http://ac.jobdu.com/problem.php?pid=1107 题目描述: 在一个果园里,小明已经将所有的水果打了下来,并按水果的不同种类分成了若干堆,小明决定把所有的水果 ...
- IOS 学习笔记 2015-04-15 手势密码(原)
// // WPSignPasswordView.h // 网投网 // // Created by wangtouwang on 15/4/9. // Copyright (c) 2015年 wan ...
- (转) UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件. 具体代码如下: #import <UIK ...