static class Program
{
[STAThread]
static void Main()
{
/* From my understanding this should install the exception handler */
Application.ThreadException += GetEventHandler();
/* Since posting this question I have found that I need to add the
following line, but even with the following line in place the
exceptions thrown are not caught.... */
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
/* Some auto generated code here */
Application.Run(new MyForm());
}
private static ThreadExceptionEventHandler GetEventHandler()
{
return new ThreadExceptionEventHandler(OnThreadException);
}
private static void OnThreadException(object sender, ThreadExceptionEventArgs e)
{
MessageBox.Show("Big error...");
}
}
根据:
http: //msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception%28v=vs.71%29.aspx
这应该有效。但是,当在类内部引发异常时,MyForm
它不会显示“大错误...”消息框,而是告诉我没有异常处理程序。任何建议,将不胜感激。