views:

98

answers:

3

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:

System.InvalidOperationException System.Data.SqlClient.SqlException

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?

Thanks

+3  A: 

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.

Mitch Wheat
If you have enough catch blocks on a single try that you need to list them in priority order in order to understand it, I would assume your code needs refactoring. :-P
technophile
@technophile : another good point :)
Mitch Wheat
+5  A: 

The order is irrelevant unless one of the exceptions named in a catch handler inherits from another exception named in another handler, in which case the more-derived catch should come first (or it will never be called).

The runtime will go through the catch blocks in the order they are listed, executing only the first one that matches, so if you have the following, only the first catch block will be executed, even if an ArgumentException is thrown:

try
{
  throw new ArgumentException("foo");
}
catch (Exception ex)
{
}
catch (ArgumentException aex)
{
  // Will never be executed.
}
technophile
+1. for mentioning the most derived part.
Mitch Wheat
+4  A: 

(I agree with Mitch, catch in order of priority for your circumstances as it makes the code more maintainable.)

I also want to add that you must catch in order of inheritance from lowest subclass to highest parent class.

for example, If you want to catch System.Exceptions and System.InvalidOperationExceptions, you must catch the InvalidOperationException first, because it is a subclass of System.Exception. If you have the catch for System.Exception first that would take precedence and your catch block for InvalidOperationException would never get triggered.

Simon P Stevens
+1. for mentioning the most derived part.
Mitch Wheat
This I already know. Exception is the most generic type (all exceptions inherit from it), so along the chain, a handler above will likely (But maybe not always) catch the exception. What I am wondering is, the two exceptions in my opening post of this thread are disparate/not related, so I guess in that case I order them in priority or perhaps likelihood?
dotnetdev