ThreadAbortException - Why is it rethrown?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a question regarding ThreadAbortException. Why is the thread abort
exception rethrown at the end of a catch clause?
Why is ThreadAbortException's behavior designed to be this way?

Thanks in advance
-Vivek
 
Hi Vivek,

MSDN reads: "ThreadAbortException is a special exception that can be caught,
but it will automatically be raised again at the end of the catch block."
 
Vivek said:
I have a question regarding ThreadAbortException. Why is the thread abort
exception rethrown at the end of a catch clause?

So that the thread will be actually *be* aborted unless it explicitly
says that it shouldn't be. There's plenty of code which catches all
exceptions but doesn't really want to stop a thread from being aborted.
Why is ThreadAbortException's behavior designed to be this way?

Because otherwise Thread.Abort would be even more useless than it
already is.
(See http://www.pobox.com/~skeet/csharp/threads/abort.shtml for my
views on it.)
 
Thanks for your views.
Now that i think more about it I wonder if it has anything to do with giving
functions upper in the call stack a chance to free unmanaged resourecs on
their call stack if any!
Example
foo1 is a method that the thread is executing (basically foo1 is the
ThreadStart parameter)
foo1 calls foo2 . foo2 opens some files (unmanaged calls) and then calls foo3
Assume we call the Abort method while foo3 is executing
If execution concluded in the ThreadAbortException of foo3 the files would
still be open!

Maybe that's the reason by ThreadAbortException is always rethrown unless
ResetAbort is explicitly called?

Any thoughts?
 
Vivek said:
Thanks for your views.
Now that i think more about it I wonder if it has anything to do with giving
functions upper in the call stack a chance to free unmanaged resourecs on
their call stack if any!

Sort of.
Example
foo1 is a method that the thread is executing (basically foo1 is the
ThreadStart parameter)
foo1 calls foo2 . foo2 opens some files (unmanaged calls) and then calls foo3
Assume we call the Abort method while foo3 is executing
If execution concluded in the ThreadAbortException of foo3 the files would
still be open!

Execution couldn't end there - the stack will have to be unwound, and
that's why the exception is rethrown. If just catching
ThreadAbortException stopped it from being rethrown, the thread
wouldn't be aborted at all. It's not that execution would stop there -
it would continue just like with any other exception.
 
Back
Top