471,582 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,582 software developers and data experts.

ThreadAbortException - Why is it rethrown?

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
Nov 17 '05 #1
4 2786
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."

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Vivek" <Vi***@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com...
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


Nov 17 '05 #2
Vivek <Vi***@discussions.microsoft.com> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
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?
"Jon Skeet [C# MVP]" wrote:
Vivek <Vi***@discussions.microsoft.com> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #4
Vivek <Vi***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Anushya | last post: by
6 posts views Thread by David Waz... | last post: by
3 posts views Thread by Steve - DND | last post: by
1 post views Thread by Burak Gunay | last post: by
3 posts views Thread by =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post: by
reply views Thread by Zeba | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by lumer26 | last post: by
1 post views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.