Question

MSDN says, for the Thread.Abort method-

When this method is invoked on a thread, the system throws a ThreadAbortException in the thread to abort it. ThreadAbortException is a special exception that can be caught by application code, but is re-thrown at the end of the catch block unless ResetAbort is called. ResetAbort cancels the request to abort, and prevents the ThreadAbortException from terminating the thread. Unexecuted finally blocks are executed before the thread is aborted.

So the exception would be thrown only once from the most immediate catch block, or from all encapsulating catch blocks ? Also, when saying unexecuted finally blocks are executed, does it include both totally unexecuted and partially executed blocks?

Was it helpful?

Solution

The finally blocks are always fully protected from ThreadAbortException both if they are executing and if they would need to be executed during the rollback of the stack.

And yes, clearly the ThreadAbortException will be automatically re-raised at the end of any try... catch block in the same thread that intercepts it, otherwise it would be meaningless. What use would it have to have a ThreadAbortException with only two "lives"?

Would it be logical that this could stop a ThreadAbortException?

try
{
    try
    {
        // The Thread.Abort "happens" here
        Thread.Sleep(Timeout.Infinite);
    }
    catch
    {
    }
}
catch 
{
}

while this wouldn't?

try
{
    // The Thread.Abort "happens" here
    Thread.Sleep(Timeout.Infinite);
}
catch
{
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top