Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the deal with the hidden Throw when catching a ThreadAbortException?

I'm going through a book of general c# development, and I've come to the thread abort section.

The book says something along the lines that when you call Thread.Abort() on another thread, that thread will throw a ThreadAbortException, and even if you tried to supress it it would automatically rethrow it, unless you did some bs that's generally frowned upon. Here's the simple example offered.

using System;
using System.Threading;

public class EntryPoint
{
    private static void ThreadFunc()
    {
        ulong counter = 0;
        while (true)
        {
            try
            {
                Console.WriteLine("{0}", counter++);
            }
            catch (ThreadAbortException)
            {
                // Attempt to swallow the exception and continue.
                Console.WriteLine("Abort!");
             }
        }
    }

    static void Main()
    {
        try
        {
            Thread newThread = new Thread(new ThreadStart(EntryPoint.ThreadFunc));
            newThread.Start();
            Thread.Sleep(2000);

            // Abort the thread.
            newThread.Abort();

           // Wait for thread to finish.
           newThread.Join();
        }
       catch (Exception e)
       {
           Console.WriteLine(e.ToString());
       }
    }
}

The book says:

When your thread finishes processing the abort exception, the runtime implicitly rethrows it at the end of your exception handler. It’s the same as if you had rethrown the exception yourself. Therefore, any outer exception handlers or finally blocks will still execute normally. In the example, the call to Join won’t be waiting forever as initially expected.

So i wrapped a try catch around the Thread.Abort() call and set a break point, expecting it to hit this, considering the text says "any outer exception handlers or finally blocks will still execute normally". BUT IT DOES NOT. I'm racking my brain to figure out why.

Anyone have any thoughts on why this isn't the case? Is the book wrong?

Thanks in advance.

like image 365
priehl Avatar asked May 13 '10 21:05

priehl


3 Answers

The exception is thrown on the thread that is aborted. Once thrown an exception travels up the call stack of that thread, but it does not jump over to another thread, nor to the caller of Thread.Abort.

The claim was:

Therefore, any outer exception handlers or finally blocks will still execute normally.

A better test of this claim is the following code:

private static void ThreadFunc()
{
    ulong counter = 0;
    while (true)
    {
        try
        {
            try
            {
                Console.WriteLine("{0}", counter++);
            }
            catch (ThreadAbortException)
            {
                // Attempt to swallow the exception and continue.
                Console.WriteLine("Abort!");
            }
        }
        catch (ThreadAbortException)
        {
            Console.WriteLine("Do we get here?");
        }
    }
}

If ThreadAbortException were a normal type of exception we would not expect to hit the line "Do we get here?", but we do.

like image 81
Mark Byers Avatar answered Nov 07 '22 02:11

Mark Byers


The Thread.Abort() doens't throw the exception. Rather, an exception is thrown in the thread, and even if you catch it, is is immediately rethrown. In your case, it would be rethrown right after it prints "Abort!". Go ahead and wrap the body of your thread method in another try/catch and you'll be able to confirm this.

like image 36
Steven Sudit Avatar answered Nov 07 '22 01:11

Steven Sudit


You probably blinked your eyes too fast to see it. Modify your code like this:

       // Wait for thread to finish.
       newThread.Join();
       Console.ReadLine();

Possible output:

....
8807
8808
Abort!
like image 22
Hans Passant Avatar answered Nov 07 '22 01:11

Hans Passant