For many methods in .NET, the exceptions they can potentially throw can be as many as 7-8 (one or two methods in XmlDocument, Load() being one I think, can throw this many exceptions).
Does this mean I have to write 8 catch blocks to catch all of these exceptions (it is best practise to catch an exception with a specific exception block and not just a general catch block of type Exception).
How do I use this information?
Thanks
Catching exceptions simply is one method to control the flow of your program. You are correct in stating that in general catching specific exceptions is better than trapping more generic exception types (all the way up to
Exception), but that does not mean it makes sense to trap all exceptions. Certain exceptions should not be trapped at all; in such a situation, you should have some error reporting system that informs you of such truly exceptional behavior.I tend to catch exceptions for which I can provide some recourse. So I will catch specific exceptions where I can either provide some useful information to the user (or caller) or when it makes sense to execute some additional logic. But oftentimes it is best to let certain exceptions not be handled at all. Many .NET exceptions – such as
ArgumentNullException– exist to inform the programmer of a programming error, not the user of some incorrect behavior.