当前位置:  编程技术>.net/c#/asp.net

C# WinForm捕获全局变量异常 SamWang解决方法

    来源: 互联网  发布时间:2014-10-15

    本文导语:  许多小公司的项目都缺少异常处理模块,我们也是。经常会出现这种情况,用户在UI界面操作,就直接跳出堆栈调用的异常信息对话框,老板看到那叫一个火啊!你们的代码怎么天天出现乱码。呵呵!这就是没有异常捕获处理...

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

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

static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
try
{
//设置应用程序处理异常方式:ThreadException处理
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

#region 应用程序的主入口点
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
#endregion
}
catch (Exception ex)
{
string str = GetExceptionMsg(ex,string.Empty);
MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}


static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
//LogManager.WriteLog(str);
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
//LogManager.WriteLog(str);
}

///
/// 生成自定义异常消息
///
/// 异常对象
/// 备用异常消息:当ex为null时有效
/// 异常字符串文本
static string GetExceptionMsg(Exception ex,string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************异常文本****************************");
sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());
if (ex != null)
{
sb.AppendLine("【异常类型】:" + ex.GetType().Name);
sb.AppendLine("【异常信息】:" + ex.Message);
sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
}
else
{
sb.AppendLine("【未处理异常】:" + backStr);
}
sb.AppendLine("***************************************************************");
return sb.ToString();
}
}

参考:
代码如下:

static class Program
{
///
/// 应用程序的主入口点。
///
[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);

#region 应用程序的主入口点

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main());

#endregion

}
catch (Exception ex)
{
string str = "";
string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "rn";

if (ex != null)
{
str = string.Format(strDateInfo + "异常类型:{0}rn异常消息:{1}rn异常信息:{2}rn",
ex.GetType().Name, ex.Message, ex.StackTrace);
}
else
{
str = string.Format("应用程序线程错误:{0}", ex);
}

//MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogManager.WriteLog(str);
}

}

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
string str = "";
string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "rn";
Exception error = e.Exception as Exception;
if (error != null)
{
str = string.Format(strDateInfo + "异常类型:{0}rn异常消息:{1}rn异常信息:{2}rn",
error.GetType().Name, error.Message, error.StackTrace);
}
else
{
str = string.Format("应用程序线程错误:{0}", e);
}

//MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogManager.WriteLog(str);
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = "";
Exception error = e.ExceptionObject as Exception;
string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "rn";
if (error != null)
{
str = string.Format(strDateInfo + "Application UnhandledException:{0};nr堆栈信息:{1}", error.Message, error.StackTrace);
}
else
{
str = string.Format("Application UnhandledError:{0}", e);
}

//MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
LogManager.WriteLog(str);
}
}

    
 
 

您可能感兴趣的文章:

  • c#多线程更新窗口(winform)GUI的数据
  • C# WinForm中禁止改变窗口大小的方法
  • c# Winform 全窗口拖动的代码
  • 解读在C#中winform程序响应键盘事件的详解
  • c# winform 关闭窗体时同时结束线程实现思路
  • C# WinForm编程获取文件物理路径的方法
  • C# Winform 整个窗口拖动的实现代码
  • C# WinForm程序完全退出的问题解决
  • C# Winform 让整个窗口都可以拖动
  • 使用C# Winform应用程序获取网页源文件的解决方法
  • C# Winform 禁止用户调整ListView的列宽
  • C# winform编程中响应回车键的实现代码
  • WinForm下 TextBox只允许输入数字的小例子 iis7站长之家
  • C#中禁止Winform窗体关闭的实现方法
  • c# 天气预报查询(winform方法)的实现代码(图文)
  • C#实现WinForm捕获最小化事件的方法
  • c#实现DataGridView控件隔行变色(winform)的代码
  • C#中Winform窗体Form的关闭按钮变灰色的方法
  • C# Winform实现捕获窗体最小化、最大化、关闭按钮事件的方法
  • C# winform treeview添加右键菜单并选中节点的方法
  • C#中使用IrisSkin2.dll美化WinForm程序界面的方法
  • C# WinForm捕获未处理的异常实例解析
  • c# WinForm捕获全局变量异常的办法
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 在Winform和WPF中注册全局快捷键实现思路及代码
  • .Net WInform开发笔记(二)Winform程序运行结构图及TCP协议在Winform中的应用
  • WinForm相对路径的陷阱
  • Winform实现抓取web页面内容的方法
  • WinForm实现关闭按钮不可用或隐藏的方法
  • WinForm实现读取Resource中文件的方法
  • WinForm下 TextBox只允许输入数字的小例子
  • Winform跨线程操作的简单方法
  • WinForm实现移除控件某个事件的方法
  • WinForm DataGridView控件隔行变色的小例子
  • WinForm开发中屏蔽WebBrowser脚本错误提示的方法
  • WinForm窗体调用WCF服务窗体卡死问题
  • WinForm实现同时让两个窗体有激活效果的特效实例
  • WinForm子窗体访问父窗体控件的实现方法
  • c# Winform 操作INI配置文件的代码
  • 深入C# winform清除由GDI绘制出来的所有线条或图形的解决方法
  • C# WINFORM 强制让窗体获得焦点的方法代码
  • C# WinForm中Panel实现用鼠标操作滚动条的实例方法
  • C# Winform 调用系统接口操作 INI 配置文件的代码
  • WinForm特效之桌面上的遮罩层实现方法
  • 深入分析C#中WinForm控件之Dock顺序调整的详解


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3