当前位置: 移动技术网 > IT编程>开发语言>c# > C# WinForm捕获全局变量异常 SamWang解决方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
许多小公司的项目都缺少异常处理模块,我们也是。经常会出现这种情况,用户在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);
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网