问题
I'm working on a project that is implemented using plain threads
. Most of the expected exceptions in the application are handled, however, there are cases where one of the threads throws an unexpected exception and the application just crashes (the application is both I/O
and Client-Server
based so it's practically impossible to handle all the exceptions).
To fix this, I'm trying to define a global UnhandledExceptionHandler
so that the application displays a friendly message instead of crashing. This is what I tried:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
// The rest of the startup logic goes here
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Utilities.DisplayUnhandledException((Exception)e.ExceptionObject);
}
}
This doesn't work though. The CurrentDomain_UnhandledException
is never called. Unfortunately, I can't change the structure of the application, which means I can't use the Task Parallel Library. I can't figure out why this doesn't work. Is there any other way to handle exceptions thrown in threads? Any help is appreciated.
回答1:
Your approach is correct. However, you will not be able to stop your application from terminating.
When an unhandled exception is thrown on a thread you have created in your application CurrentDomain_UnhandledException
will be called allowing you to log or report the exception. However, unless the e.IsTerminating
is false
you will not be able to stop your application from terminating. You can read more about this behavior in Exceptions in Managed Threads.
If you find that CurrentDomain_UnhandledException
is never called you should verify that Application_Startup
is called to setup the handler.
If you still are having problems you should verify that Utilities.DisplayUnhandledException
does not throw an exception. This will also lead to immediate termination of your application. In particular, if e.ExceptionObject
is not of type Exception
casting it to Exception
will throw an exception. However, under normal circumstances, when the exception object is not a managed exception, it will be wrapped in a RuntimeWrappedException.
To avoid termination of your application you need to catch the exception at "the top of the stack" of your thread method. If that is not possible because you do not have access to the code then the unhandled exceptions are an indication of buggy software, and even though it is inconvenient the best thing to do when a software bug is discovered is to terminate the application to avoid corruption.
来源:https://stackoverflow.com/questions/17203316/unhandled-exceptions-thrown-in-threads-are-not-caught