C# – Order of catch clauses


When using a framework method, the description you get when you highlight over it will list the exception(s) possibly raised.

Does it matter if you write the catch handlers in this order? E.g. SqlConnection.Open() can throw:


It is listed in that order. Should my catch handlers follow this order? If so, why?

Also, some methods such as SqlDataAdapter.Fill() return an int but no error is reported by Visual Studio if I don't have a variable to store the return value. Why?


Best Solution

You should place your catch handlers not in the order they are listed in the documentation, but in the order of priority for your particular circumstances.

In saying that, if you are accessing a SQL Server database and get a SQL exception then it is unlikely your application will be able to continue, so the SqlException should probably be high priority.

Update: I omitted the part about the order of Exception inheritance (from most derived to base), as I incorrectly assumed this was a given. Other posters mentioned this explicitly.