What exceptions to expect?
I wrote a function that calls Convert.ChangeType(object val,TypeCode code) to check if a given value can be converted to the specified TypeCode. And I would like to adhere to the rule "Catch only the exceptions that you expect". But what can one expect? Initially I expected 2 types of exceptions: InvalidCastException and FormatException. Then in a test case I found out that
OverflowException is also occurs in some cases. So, I added another catch block...
You see, I am not even 50% sure that I am handling all the possible exception types. I guess, I could spend a few days reading documentation and investigating with Reflector. And even dissambly can not help in some cases where unmanaged code is used (this applies to the example). So, is it worth the effort? Or better just do catch(Exception ex)? If I catch a generic exception, it might turn out to be a memory exception. And sure, it is not the best idea to say that the format of val was invalid, when you are out of memory. But since I can't recover from memory exceptions, what's the point of handling those?