Dec 31 2005

Exception Management in Threads

Category: UncategorizedAmit Bahree @ 12:18 am

Exception Management is a topic near and dear to me personally, primarily because of the lack of understanding of most developers (that I have come across). There is an interesting change in the .NET 2.0 CLR on how it manages unhandled exceptions in a thread. If you write multi-threaded apps then this is important for you to understand. If you don’t write multi-threaded apps today, but I presume you would soon, then this is a good learning exercises.

In .NET 2.0 the CLR allows unhandled exceptions in threads to proceed naturally – which in many cases probably means that the application will terminate. For certain cases (listed below) where exceptions are used for controlling program flow, the CLR does provide a backstop for unhandled exceptions in threads. Note, the CLR terminated the threads in these cases and does not propagate the exception further.

  • Abort() is called causing a ThreadAbortException.
  • AppDomain (in which the thread is running) is being unloaded, causing a AppDomainUnloadedException.
  • The Host process (or CLR) terminates the thread via an internal exception.

This is a significant change from .NET v1.x where there is no concept of a Unhandled Exception in many situations such as a Thread pool. If an unhandled exception is thrown, the runtime prints that to the console and then returns the thread to the thread pool. Also, if an unhandled exception occurs in the Start() method of a Thread class, the runtime again prints the exception stack trace to the console and then gracefully terminating the thread. Lastly, if an unhandled exception occurs on a Finalizer thread, the trace is written to the console, and the finalizer thread is allowed to resume!

Needless to say, if you have code currently designed for this, it will break in .NET 2.0 and you would need to change your implementation. As a temporary workaround you can choose your app to still use the old style from v1.x by setting the flag in the runtime section, but this should be strictly till such a time you can port the code over to the new version:

<legacyUnhandledExceptionPolicy enabled=”1″/>

If you got MSDN installed for VS 05, you can read more here then.

Share
Similar posts to check out:
  • February 3, 2009 -- Exceptions Management (or lack of it) (0)
    A good example of what not to do. ...
  • November 13, 2006 -- DragMove (0)
    Did you know there is a new method called DragMove which is part of the Window class and allows you to move the window using a mouse? I certainly did not - till now. This is part of .NET 3.0, specifically part of WPF and . Here is an example from the SDK:protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); // Begin dragging the window this.DragMove(); } ...
  • January 3, 2006 -- Writing High-Perf. Managed Apps? (0)
    If you are writing high-perf. managed applications, all the old-world advice from the days of good old COM is still valid. But you do need to think of a few other things such as how the CLR JIT would change things, or the Exception Handling, or Threading and syncing up, or possibly the GC and Allocation Profiles? While this MSDN article is old (now), it still is excellent Food for Thought when it comes to writing high-perf. apps....

Tags:

Leave a Reply

*

Get Adobe Flash player