Wednesday, February 6, 2008

.NET 2.0 Exception Handling in Threads

public static void Main()
{
new Thread (Go).Start();
}

static void Go()
{
try
{
...
throw null; // this exception will get caught below
...
}
catch (Exception ex)
{
Typically log the exception, and/or signal another thread
that we've come unstuck
...
}

From .NET 2.0 onwards, an unhandled exception on any thread shuts down the whole application, meaning ignoring the exception is generally not an option. Hence a try/catch block is required in every thread entry method – at least in production applications – in order to avoid unwanted application shutdown in case of an unhandled exception.

No comments:

Check This Out!

More Links to Good Information