using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading;namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //主线程执行这个代码,程序是不会直接退出的,而是会弹出[异常信息对话框] private void button1_Click(object sender, EventArgs e) { ExceptionMethod(); } //new的线程里执行这个代码,程序立即退出,没有任何提示,如何做到[[不退出]]呢 private void button2_Click(object sender, EventArgs e) { //在线程启动的外面try catch也是没用的,程序也是立马退出 try { new Thread(ExceptionMethod).Start(); } catch (Exception ex) { throw ex; } } //可能包含异常的方法 //当然在这个方法里面处理异常是最好的,但是我的方法里做的事情非常多, //我只有在调试模式下,运行很长时间才能发现异常, //因为直接运行程序的状态下,发生异常就退出了,所以我无法知道异常出在哪里 void ExceptionMethod() { int a = 0; int c = 10 / a; } } }
luminji大大提出的//处理非UI线程异常 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 这个确实可以捕获到线程里发生的异常了,但是调用了处理方法CurrentDomain_UnhandledException之后,程序还是无提示退出....不过已经不错了,至少我可以在程序的运行过程中,来获取异常信息并写入日志了.
int a=.....;
if(a==0)
throw new Exception("除零错误");
else
a=10/a;
WINFORM未处理异常之捕获
//处理未捕获的异常
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
#region 处理未捕获异常的挂钩函数
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
Exception error = e.Exception as Exception;
if (error != null)
{
LogRecord.putErrorLog(string.Format("出现应用程序未处理的异常\n异常类型:{0}\n异常消息:{1}\n异常位置:{2}\n",
error.GetType().Name, error.Message, error.StackTrace), "main");
}
else
{
LogRecord.putActionLog(string.Format("Application ThreadError:{0}", e));
}
} static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
cn.ccets.papercontrol.FullScreenHandle.ShowTray();
Exception error = e.ExceptionObject as Exception;
if (error != null)
{
LogRecord.putActionLog(string.Format("Application UnhandledException:{0};\n堆栈信息:{1}", error.Message, error.StackTrace));
}
else
{
LogRecord.putActionLog(string.Format("Application UnhandledError:{0}", e));
}
} #endregion
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
Mutex mutex = new Mutex(false, "mainticket");
bool runEnabled = mutex.WaitOne(0, false); if (AppAdminGlobal.Login())
{
Application.Run(new MainForm());
}
} static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.ToString());
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//主线程执行这个代码,程序是不会直接退出的,而是会弹出[异常信息对话框]
private void button1_Click(object sender, EventArgs e)
{
ExceptionMethod();
} //new的线程里执行这个代码,程序立即退出,没有任何提示,如何做到[[不退出]]呢
private void button2_Click(object sender, EventArgs e)
{
//在线程启动的外面try catch也是没用的,程序也是立马退出
try
{
new Thread(ExceptionMethod).Start();
}
catch (Exception ex)
{ throw ex;
}
}
//可能包含异常的方法
//当然在这个方法里面处理异常是最好的,但是我的方法里做的事情非常多,
//我只有在调试模式下,运行很长时间才能发现异常,
//因为直接运行程序的状态下,发生异常就退出了,所以我无法知道异常出在哪里
void ExceptionMethod()
{
int a = 0;
int c = 10 / a;
} }
}
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
这个确实可以捕获到线程里发生的异常了,但是调用了处理方法CurrentDomain_UnhandledException之后,程序还是无提示退出....不过已经不错了,至少我可以在程序的运行过程中,来获取异常信息并写入日志了.
static void Main()
{
try{new Thread(delegate(){int i= 1/0;}).Start();} catch{}
}
目前找到的最好的答案也就是
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
但结果是程序依然会挂掉。
在MSDN中说“此事件的 UnhandledExceptionEventHandler 委托为未捕获的异常提供默认处理。在未对此事件处理时,系统默认处理程序将异常报告给用户,并终止应用程序。”
问题是如何“对此事件处理”让程序不会终止。
持续关注中。
我这里有一个后台程序,调用别人写很多个dll(我没有dll的源代码),每个dll用于处理一类业务,每种处理都在各自的线程中运行。我无法保证别人写的代码里有做好异常处理,但我又不希望任何一个线程中出现异常导致整个后台进程终止,毕竟这几类业务是相互独立的。就是这个问题我还没找到方法解决。
方法笨拙点,但是很实用