Welcome to MSDN Blogs Sign in | Join | Help

Unhandled Exception handler (UnhandledExceptionEventHandler) to collect more information about unexpected exceptions

 

To continue on the exception post from last week I did a bit more research and learnt some more. I thought that I will share that with everyone. We had some interesting conversations in our team about exception handling and the following thought came up.

.NET gives you an option to write an unhandled exception handler which can be used to catch exceptions that you did not expect in your code and hence did not handle in your catch block. These are errors in the code as you did not expect them and should fail the application. This interestingly works even if you “throw;” or “throw new Exception(..)” from within the catch block.

It will be a good pattern to use this to log information about all unexpected exceptions in the application without introducing a “catch(Exception e)” in the code.

The sample below demonstrates the code for the exception handler. If you uncomment the throw blocks one then the handler will be invoked when the exception is thrown. Does anyone use this pattern and have benefited from it? Are there other ways to do this? Love to hear your thoughts!

 

using System;

public class Sample

{

   public static void Example()

   {

      AppDomain currentDomain = AppDomain.CurrentDomain;

      currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

 

      try

      {

         throw new Exception("First Handled Exception");

      }

      catch (Exception e)

      {

         Console.WriteLine("Catch clause caught : " + e.Message);

      }

 

      try

      {

          throw new Exception("Second Handled Exception");

      }

      catch (Exception e)

      {

          Console.WriteLine("Catch clause caught : " + e.Message);

          // throw;

          // throw new Exception("Re-throw second exception", e);

      }

 

      // throw new Exception("Un-Handled Exception");

   }

 

   static void MyHandler(object sender, UnhandledExceptionEventArgs args)

   {

      Exception e = (Exception) args.ExceptionObject;

      Console.WriteLine("Exception Message: {0}", e.Message);

   }

 

   public static void Main()

   {

      Example();

   }

}

Published Wednesday, April 30, 2008 6:27 PM by thottams@microsoft.com

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Airline Travel » Unhandled Exception handler (UnhandledExceptionEventHandler) to collect more information about unexpected exceptions

# re: Unhandled Exception handler (UnhandledExceptionEventHandler) to collect more information about unexpected exceptions

The biggest problem with this is that the exceptions are no longer considered "Unhandled" by Visual Studio when running in the debugger.  

As such, it won't break until after your unhandled handler runs which then loses the original call stack.

In our applications, we use an #if !DEBUG to enable the unhandled handler, giving us the best of both worlds.

Wednesday, April 30, 2008 6:05 PM by onovotny

# re: Unhandled Exception handler (UnhandledExceptionEventHandler) to collect more information about unexpected exceptions

Didn't know that this causes visual studio to think the exception to be handled. I have to try this, but looks like the DEBUG is a good work around!

Friday, May 02, 2008 12:18 PM by Thottam Sriram

# re: Unhandled Exception handler (UnhandledExceptionEventHandler) to collect more information about unexpected exceptions

Thottam, Meet Larry:

http://blogs.msdn.com/larryosterman/archive/2008/05/01/resilience-is-not-necessarily-a-good-thing.aspx

He blogged about Eric's post:

http://blogs.msdn.com/eric_brechner/archive/2008/05/01/crash-dummies-resilience.aspx

Interesting that so many 'softies are blogging about handling unexpected exceptions (or not).

Friday, May 02, 2008 6:32 PM by Drew

# re: Unhandled Exception handler (UnhandledExceptionEventHandler) to collect more information about unexpected exceptions

Thanks Drew for the link. So close and yet I have not read his blog before. Interesting articles about exceptions and others too!

Thursday, May 15, 2008 2:05 PM by Thottam Sriram

# re: Unhandled Exception handler (UnhandledExceptionEventHandler) to collect more information about unexpected exceptions

I'm not a programmer; I got an error message titled "Microsoft .NET Framework and starting "an unhandled exception..." and I can't remove it from my display. Is it possible to fix it, delete it, etc. without knowing how to program? I tried the "restore" but it didn't.

Tuesday, July 01, 2008 9:32 AM by mark

# re: Unhandled Exception handler (UnhandledExceptionEventHandler) to collect more information about unexpected exceptions

I don't understand your scenario completely, but let me try. Right click on your task bar and select Task Manager. Select the Applications tab and you should be able to end this error message dialog from there if it does not have any close button on it which I doubt.

Wednesday, July 02, 2008 9:56 PM by Thottam Sriram

# re: Unhandled Exception handler (UnhandledExceptionEventHandler) to collect more information about unexpected exceptions

Umm, this is a joke, right? And you work at Microsoft?

Your blog post comes like 6 years too late and illustrates nothing. Actually, it will do more harm than it helps other people.

What you published may work in the special case presented, but it isn't anyway near what is considered to be best practice.

Friday, August 01, 2008 2:59 AM by MikeP

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker