catch (ThreadAbortException)
{ }
catch (Exception ex)
{
TraceManager.TraceException(ex,
(int)ErrorCode.GENERIC_EXCEPTION,
ex.StackTrace + "\n" + ex.Message + "\n" + VendorUrl);
}
does it make sense to even have the
catch (ThreadAbortException)
{ }
or will that cause the ThreadAbortException
to be swallowed and lost forever?
ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block. When this exception is raised, the runtime executes all the finally blocks before ending the thread.
If the thread that calls Abort holds a lock that the aborted thread requires, a deadlock can occur. If Abort is called on a thread that has not been started, the thread will abort when Start is called. If Abort is called on a thread that is blocked or is sleeping, the thread is interrupted and then aborted.
ThreadAbortException
cannot be caught "completely"; it will automatically be rethrown at the end of the catch
block (see the linked MSDN docs page) unless Thread.ResetAbort
is called first.
So, the only sensible catch
block would be:
catch (ThreadAbortException)
{
// possibly do something here
Thread.ResetAbort();
}
But this has a really evil smell. There's probably no reason to do it, so you may want to rethink your approach.
Update:
There are many questions on SO that deal with Thread.Abort
:
This one has the same answer as I have given here.
This one has an answer that expands on "don't ever call Thread.Abort
unless Cthulhu is rising" (which I toned down considerably to an "evil smell").
There are also many others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With