There is an example in the C# MS documentation that explains how to use this to capture unhandled events. It proposes the code:
Thread newThread = null;
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
public static void Main(string[] args)
{
Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.Run(new ErrorHandlerForm());
}
private void button1_Click(object sender, System.EventArgs e)
{
throw new ArgumentException("The parameter was invalid");
}
private void button2_Click(object sender, System.EventArgs e)
{
ThreadStart newThreadStart = new ThreadStart(newThread_Execute);
newThread = new Thread(newThreadStart);
newThread.Start();
}
void newThread_Execute()
{
throw new Exception("The method or operation is not implemented.");
}
private static void Form1_UIThreadException(object sender, ThreadExceptionEventArgs t)
{
DialogResult result = DialogResult.Cancel;
try
{
result = ShowThreadExceptionDialog("Windows Forms Error", t.Exception);
}
catch
{
try
{
MessageBox.Show("Fatal Windows Forms Error",
"Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
if (result == DialogResult.Abort)
Application.Exit();
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;
string errorMsg = "An application error occurred. Please contact the adminstrator " +
"with the following information:\n\n";
if (!EventLog.SourceExists("ThreadException"))
{
EventLog.CreateEventSource("ThreadException", "Application");
}
EventLog myLog = new EventLog();
myLog.Source = "ThreadException";
myLog.WriteEntry(errorMsg + ex.Message + "\n\nStack Trace:\n" + ex.StackTrace);
}
catch (Exception exc)
{
try
{
MessageBox.Show("Fatal Non-UI Error",
"Fatal Non-UI Error. Could not write the error to the event log. Reason: "
+ exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
}
private static DialogResult ShowThreadExceptionDialog(string title, Exception e)
{
string errorMsg = "An application error occurred. Please contact the adminstrator " +
"with the following information:\n\n";
errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
return MessageBox.Show(errorMsg, title, MessageBoxButtons.AbortRetryIgnore,
MessageBoxIcon.Stop);
}
I have worked through this a few times without any luck.
I am primarily interested in using the UI ThreadException.
I have added:
Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
And created a simple Form1_UIThreadException handler. When I test this the exception thrown by button1 is still not caught.
I can see that C# is including the code and the handler but the Exception mechanism does not seem to fire for me.
If I simply use a try/catch on my Application.Run(new Formi()); in program.cs I can catch the Exceptions that bubble up.
I do have to comment out the ThreadException references first or that does not work. So it is in effect but not firing.
Has anyone worked through this example? Can you offer what I may be missing? :sigh:
Thanks.