This blog has moved to Medium

Subscribe via email


Unhandled Exceptions Crash .NET Threads

A little something I learned at DSM today. It appears if any thread in .NET crashes (lets a thrown exception fly through the top stack level), the process crashes. I refused to believe at first, but testing on .NET 2.0 showed it to be true:

(I should really switch to another blog platform, I didn’t find a decent way to write code in Blogspot).

class ThreadCrashTest
{
  static void Main()
  {
    new Thread(Foo).Start();
    for (int i = 0; i < 10; ++i)
    {
      Console.WriteLine(i);
      Thread.Sleep(100);
    }
  }
 
  private static void Foo()
  {
    Console.WriteLine("Crashing");
    throw new Exception("");
  }
}

According to Yan, the behavior on .NET 3 is to crash the AppDomain instead of the entire process.