Catching 'Exception' is Bad? Isn't It?
We all know that catching Exception is a bad idea - for all sorts of reasons.
Nick Guerrera has written a good series of blog posts ( Part 1, 2, 3 ) talking about the dangers of attempting to handle any (every) kind of exception and why FxCop warns against code that does this.
The crux of Nick's argument is that there's no way of writing a generic handler that does the right thing to handle every case. He's right. Code that handles an InvalidOperationException must be different from code that handles an SmtpException.
Mustn't it.
Except that I've been writing a lot of code like this:
public void Warble(string name) { try { // Complex processing goes here ... } catch(Exception ex) { const string Template = "While Warbling for {0}"; string message = string.Format( Template, name); throw new InvalidOperationException(message, ex); } }
The point of this code is to add context to the original error . Whether the original exception is an ArgumentNullException, an InvalidOperationException or something else, the new exception gives more information about what was happening when the problem occurred.
Importantly, the information represented by the original exception isn't lost - as the original is retained as the InnerException within the new exception.
Of course, FxCop complains about this code, as it's a technical violation of the rule "Do Not Catch General Exception Types".
But, is this actually bad code?

Comments
OutOfMemoryException
What about an OutOfMemoryException, NullReferenceException or another similar exception that you can really handle. Won't this cover those up?
The usual argument against catching Exception is that there are some excetions that need to cause the program to exit, with some cleanup/logging done first if necessary.
Matt
It's about logging
It's exactly for the case where an OutOfMemoryException, NullReferenceException or similar that is thown that I want to add the additional context.
So often these errors only occur with specific data - or for a specific edge case. Knowing the context when the problem occurs gives me a much better opportunity to fix the root cause of the problem instead of just patching the symptom.
You raise a good point, though - I tend to write defensive code that throws very few exceptions, and in most cases when I do handle an exception it's right at the point where the exception is thrown. It would be a "bad thing" if wrapping a serious exception in the way I've described resulted in an attempt to recover, instead of simply logging the outcome.
Another thing is that
Another thing is that
throw new InvalidOperationException(message, ex);can cause you to lose some information about the stack trace, see http://stackoverflow.com/questions/22623/net-throwing-exceptions-best-pr... for more info.
If you want to catch all unhandled exceptions in your program you can use Application event-handlers, see http://www.codeproject.com/KB/exception/ExceptionHandling.aspx for more info. Then you can log all of them in 1 place without having to add code like you've put in your sample, in several places in your app. The only thing is that this won't give you the "context" you talk about, but it will give you a complete stack trace if you have PDB symbols included.
Keeping the Stack Trace
It is true that the stack trace of the new exception only starts at the point where the exception is thrown. However, retaining the original exception (as the InnerException) means that no stack information is really lost.
Although, if the global exception handler doesn't look for, and process, Inner Exception, information will be lost at that point.
Good observations - thanks.