Geeks With Blogs

News

View Anthony Trudeau's profile on LinkedIn

Add to Technorati Favorites


Anthony Trudeau

Probably all of us know now that you shouldn't catch general exceptions (CA1031).  There are practical reasons for this rule such as the fact that you really cannot handle an OutOfMemoryException or StackOverflowException for example.  Here is an example of catching a general exception for those that might not know what I'm talking about.

   try

   {

   }

   catch (Exception ex)

   {

   } 

There are also more intrinsic design reasons why you shouldn't catch a general exception.  And if there is no other reason, if you're catching a general exception can you say you really know your code?  And if you don't know your code maybe it's time to step back and put a little more thought into what you're doing.

But, wait, what if you know your code but the code your try block is nesting isn't known -- such as calling an abstract member within a base class?  Still, I say no general exceptions.  Its better to let the application come tumbling down then the possible side effects of catching and either handling or swallowing an exception that you really don't know how to properly handle.  Okay you say, what if its imperative that the application stay alive despite the possibility of ill-behaving code in a concrete class?  Fine you got me there, its time to catch the general exception -- with the caveat that you still need to properly handle the exception.

How do you handle a general exception, because by its definition it could be virtually anything?  You really cannot, but you can handle quite a bit and remember that by virtue of this application that must have high reliability (be up all of the time).  When you have an application like that you probably have some type of service managing the class.  Therefore, what I do is add an event to my class called UnhandledException.

The service class can subscribe to that event and choose to handle it or not via a Handled property on a concrete implementation of the EventArgs class.  With that in mind you just need to address your initial error handler and make sure that anything that isn't handled by the service class is then re-thrown which will and should take down the whole shebang.

   try

   {

       // some process that can raise an unknown exception

   }

   catch (Exception ex)

   {

       ExceptionEventArgs args = new ExceptionEventArgs(ex);

       OnUnhandledException(args);

 

       if (!args.Handled)

          throw;

   }

 

As you can see from this implementation we leave it up to the service class to determine what to do with the exception.  You still may have tasks to do with specific exceptions and in theses cases you should have blocks for those specific exceptions.  However, this cleanly delegates the work to the service allowing it to make the determination of what to do -- which may include tearing down and recycling the instance that raised the exception.

Posted on Saturday, April 26, 2008 3:02 AM .NET | Back to top


Comments on this post: Catching General Exceptions

# re: Catching General Exceptions
Requesting Gravatar...
An excellent approach. I have used a variation on the posted implementation for years.

The only difference is I have encapsulated all of the logic into an extensible class. This prevents the duplication associated with having the implementation inline.

It also makes it easy to have logging, recovery handling, etc....

For a general overview of the concepts, get the Exception Handling Block from Microsoft Patterns and Practices....
Left by TheCPUWizard on Apr 26, 2008 2:09 PM

# re: Catching General Exceptions
Requesting Gravatar...
I think that or using an extension method (if working with a class hierarchy) would be an great choice. As far as the use of the EHB, I would push the use of that to the service class to cut down on coupling.

My example was purposelly general for the sake of explaining the point.
Left by Anthony Trudeau on Apr 26, 2008 2:49 PM

Your comment:
 (will show your gravatar)


Copyright © Anthony Trudeau | Powered by: GeeksWithBlogs.net