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

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

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


代码如下:

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

/// <summary>
/// 生成自定义异常消息
/// </summary>
/// <param name="ex">异常对象</param>
/// <param name="backStr">备用异常消息:当ex为null时有效</param>
/// <returns>异常字符串文本</returns>
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
{
/// <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);

#region 应用程序的主入口点

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

#endregion

}
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);
}

//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() + "\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);
}

//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() + "\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);
}

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

(0)

相关推荐

  • C# Winform实现捕获窗体最小化、最大化、关闭按钮事件的方法

    本文实例讲述了C# Winform实现捕获窗体最小化.最大化.关闭按钮事件的方法,主要是通过重写WndProc来实现的.分享给大家供大家参考.具体方法如下: 主要功能代码如下: const int WM_SYSCOMMAND = 0x112; const int SC_CLOSE = 0xF060; const int SC_MINIMIZE = 0xF020; const int SC_MAXIMIZE = 0xF030; protected override void WndProc(ref

  • C#实现的鼠标钩子

    C#实现的鼠标钩子,可以获取鼠标在屏幕中的坐标,记得要以管理员权限运行才行 复制代码 代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; u

  • C# Hook钩子实例代码 截取键盘输入

    一.关于本文 以最通俗的语言说明钩子的使用方法,具体到钩子的详细介绍可以参照下面的网址: http://www.microsoft.com/china/community/program/originalarticles/techdoc/hook.mspx 二.钩子的简单介绍 从字面上理解,钩子就是想钩住些东西,在程序里可以利用钩子提前处理些Windows消息. 例子:有一个Form,Form里有个TextBox,我们想让用户在TextBox里输入的时候,不管敲键盘的哪个键,TextBox里显示

  • C#键盘鼠标钩子实例

    本文实例讲述了C#键盘鼠标钩子的实现方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.ComponentModel; using System.Ref

  • C#使用钩子获得按键信息的方法

    本文实例讲述了C#使用钩子获得按键信息的方法.分享给大家供大家参考.具体如下: 窗体相关代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runti

  • C# WinForm捕获未处理的异常实例解析

    本文以一个完整的实例形式讲述了C# WinForm捕获未处理的异常的方法.分享给大家供大家参考之用.具体代码如下: using System; using System.Collections.Generic; using System.Windows.Forms; using System.IO; namespace GobalException { static class Program { /// <summary> /// 应用程序的主入口点. /// </summary>

  • c# 钩子学习笔记

    包括系统钩子和线程钩子,或者叫全局钩子和私有钩子.系统钩子需要一个单独的DLL,这个地方耽误了不少时间,网上有说可以不用单独DLL的. 现在开始贴代码,代码参照了红黑联盟中的一篇文章. 复制代码 代码如下: public class SetHook { public class HookTypes { /// <summary> /// 钩子类型 /// </summary> public enum HookType { WH_JOURNALRECORD = 0,//对寄送至消息队

  • C#通过接口与线程通信(捕获线程状态)示例代码

    提示:本文所提到的线程状态变化,并不是指线程启动.暂停.停止,而是说线程内部状态的迁移.随着软件技术不断发展,用户需求不断提升,多线程的重要性日益凸显. 关于线程,通俗的讲,每当启动一个exe应用程序,都会创建一个进程和一个主线程,主线程用来处理界面绘制.界面事件响应等工作,而进程则是线程的容器,主线程和用户创建的新线程,都将在相应的进程中维护.由此可知,一个程序之所以能工作,是线程的功劳,进程仅仅是容器而已,一旦程序的主线程被阻塞,就会造成界面无响应等现象,这时候就要用多线程解决问题.然而,在

  • C#实现可捕获几乎所有键盘鼠标事件的钩子类完整实例

    本文实例讲述了C#实现可捕获几乎所有键盘鼠标事件的钩子类.分享给大家供大家参考,具体如下: using System; using System.Text; using System.Runtime.InteropServices; using System.Reflection; using System.Windows.Forms; namespace MouseKeyboardLibrary { /// <summary> /// Abstract base class for Mous

  • 基于C#实现的HOOK键盘钩子实例代码

    本文所述为基于C#实现的HOOK实例,该实例可用来屏蔽系统热键.程序主要实现了安装钩子.传递钩子.卸载钩子等功能.在传递钩子中: <param name="pHookHandle">是您自己的钩子函数的句柄.用该句柄可以遍历钩子链</param> <param name="nCode">把传入的参数简单传给CallNextHookEx即可</param> <param name="wParam"

随机推荐