We all know that catching
Exception is a bad idea - for all sorts of reasons.
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
Except that I’ve been writing a lot of code like this:
The point of this code is to add context to the original error. Whether the original exception is 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.
But, is this actually bad code? What do you think? Why?