4

If I have the following situation:

  1. Execute() creates a new thread and executes function GetSession() in it.
  2. Execute() executes GetSession() again in it's own thread.
  3. Execute() joins the thread from (1).

My question is:

What happens if GetSession() throws an exception from the thread spawned in (1) while the thread Execute() is running in is currently running GetSession() itself?

Does the exception re-thrown from the extra thread propagate up to Execute() and cause it to go to its handler even though it's from a different thread?


Here's some sample code to demonstrate the issue:

I just made this up in the window here (it's a mock-up), so appologies for syntax errors.

public void Execute()
{
    //Some logon data for two servers.
    string server1 = "x", server2 = "y", logon = "logon", password = "password";

    //Varialbes to store sessions in.
    MySession session1, session2;

    try
    {
        //Start first session request in new thread.
        Thread thread = new Thread(() =>
            session1 = GetSession(server1, logon, password));
        thread.Start();

        //Start second request in current thread, wait for first to complete.
        session2 = GetSession(server2, logon, password));
        thread.Join();
    }
    catch(Exception ex)
    {
        //Will this get hit if thread1 throws an exception?
        MessageBox.Show(ex.ToString());
        return;
    }
}

private MySession GetSession(string server, string logon, string password)
{
    try
    {
        return new MySession(server, logon, password);
    }
    catch(Exception Ex)
    {
        throw(Ex);
    }
}
svick
  • 236,525
  • 50
  • 385
  • 514
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
  • 1
    Perhaps an obvious question, but why not just try it? – Daniel Kelley Jul 18 '12 at 15:56
  • Actually, I had a more complex situation that boiled down to this, but the debugger kept breaking and highlighting the re-throw when the other thread hit an exception. I wasn't sure if that was due to the debugger's settings, or if the exception just wasn't supposed to propagate, so I thought I'd ask :) I'm sure I could have written a little experiment though, but it's nice to get expert answers. – John Humphreys Jul 18 '12 at 16:05

1 Answers1

5

What happens if GetSession() throws an exception from the thread spawned in (1) while the thread Execute() is running in is currently running GetSession() itself?

The threaded version will raise an unhandled exception, which will trigger AppDomain.UnhandledException. Unless this is explicitly handled there, it will tear down the application.

Does the exception re-thrown from the extra thread propagate up to Execute() and cause it to go to its handler even though it's from a different thread?

No. It will be unhandled.


Note that this is one of the advantages of the TPL. If you use Task instead of a Thread, you can make this propogate the exception back to the main thread:

try
{
    //Start first session request in new thread.
    Task<Session> task = Task.Factory.StartNew(() => GetSession(server1, logon, password));

    //Start second request in current thread, wait for first to complete.
    session2 = GetSession(server2, logon, password));
    session1 = task.Result; // This blocks, and will raise an exception here, on this thread
}
catch(Exception ex)
{
    // Now this will get hit
    MessageBox.Show(ex.ToString());
    return;
}

Note, however, that this will be an AggregateException, and requires special handling.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373