My blog has moved! Redirecting...

You should be automatically redirected. If not, visit http://ripper234.com and update your bookmarks.

09 September 2008

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.

2 comments:

Unknown said...

For code in posts, a quick fix is to use '<pre>' html blocks.

For nice syntax highlighting check out SyntaxHighlighter (usage is here)

ripper234 said...

Thanks for the tip, through I am right in the middle of a move (hush hush) to Wordpress. Will use a proper plugin there.