What happens when a .NET thread throws an exception? What happens when a .NET thread throws an exception? multithreading multithreading

What happens when a .NET thread throws an exception?


Something like:

Thread thread = new Thread(delegate() {    try    {        MyIPoller.Start();    }    catch(ThreadAbortException)    {    }    catch(Exception ex)    {        //handle    }    finally    {    }});

This will ensure the exception doesn't make it to the top of the thread.


You should catch the exception at the method you use at the top of the thread, and do the logging from there.

An unhandled exception (at the top of a thread) will (in 2.0 onwards) kill your process. Not good.

i.e. whatever method you pass to Thread.Start (etc) should have a try/catch, and do something useful in the catch (logging, perhaps graceful shutdown, etc).

To achieve this, you could use:

  • static logging methods
  • captured variables into the delegate (as an anonymous method)
  • expose your method on an instance that already knows about the logger


In .NET 4.0+ you should use Tasks instead of threads. Here's a nice article on exception handling in Task Parallel Library