I have a thread that is very simple. The only thing the Thread object does is throw new Exception();
. The reason why I did that is because I needed to test what happens if a exception happens in a separate thread in my application. I have tested this with a "real" run time exception and my problem is the same.
What I do before I call the thread is:
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
Application.Current.DispatcherUnhandledException += CurrentOnDispatcherUnhandledException;
//To handle exceptions in another thread
private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
//Do nothing ignore exception
}
//To handle all exceptions in main thread
private static void CurrentOnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs dispatcherUnhandledExceptionEventArgs)
{
dispatcherUnhandledExceptionEventArgs.Handled = true;
}
When this thread does throw a exception it first hits CurrentDomainOnUnhandledException
like it should, and then it does not hit CurrentOnDispatcherUnhandledException
. Right after CurrentDomainOnUnhandledException
The stack then moves back to the Thread where it throws the exception and throws it again, executing the same line. It never stops doing this. It is basically a never ending loop and I can not figure out why.
The UnhandledExceptionEventArgs does not have a property to set, to say we have handled the exception and I am pretty sure this is part of the problem.
What am I doing wrong, how to I catch this UnhandledException in this thread, and let the thread continue to execute (preferred) or abort / kill the thread (not preferred) so that it does not continue to execute the throw new exception line.
I have tested this with debug and release (made sure debugging info on release was set to none) and this still happens in both versions, the exception continues to happen over and over again.
Thanks in advance, I am just stumped and have no idea why it does it.