This is related to a previous question.
What I'm trying to understand now is how come UI thread exceptions can be prevented from terminating the application while non-UI exceptions can't be.
For reference, see this example.
Most importantly, what I would like to be able to do in that case is "silently" terminate the process--without displaying the Windows dialog box that asks whether I'd like to send an error report or not.
This is my AppDomain UnhandledExceptionHandler:
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
// Maybe do some logging here if allowed
}
catch
{
}
// then just terminate the application
Application.Exit();
}
UPDATE
In light of comments in this answer, I'd like to clarify that most importantly I'd like to find out more about the mechanism that enables the UI thread to have an early opportunity to catch unhandled exceptions via the Application.ThreadException
mechanism. And whether such behavior could be implemented on a non-UI thread.
After doing some more searches on Google I found this very interesting explanation that was given to the same problem as described by Jeff Atwood on his blog.
However, I'm still interested in how the UI thread accomplishes the trick of allow you to have a catch-all handler for all UI thread exceptions.
Even more, I'm very interested in a way to disable the .NET JIT Debugging Dialog for my application only (without disabling it for the whole machine as seen here)
It's not that any AppDomain exception terminates the application, it's that unhandled exceptions (of any sort) will tear down the AppDomain and terminate the application.
The issue here is that you can handle UI thread exceptions explicitly, at a fairly high level. However, when you have an unhandled exception in a background thread, there is no means of handling it easily at the same level, so it tends to propagate up and pull down the application. Application.ThreadException allows you to at least know that this is what caused the error, and log it if necessarily.
Unhandled exceptions in the UI thread will cause the same thing to happen.
Does this help at all?
Improved Unhandled Exception behavior in .NET 2.0
Also, this code seems to "die quietly". Are you looking for something else?