When using exceptions, always try to use the most accurate exception. For example, when using SQL Server, catch the SqlException, because it will contain much more information about the exception than the general exception. You can get actual line numbers and other useful pieces of diagnostic information.
Once you have caught and registered everything that is relevant, discard the exception or wrap it in a less specific exception, such as an InvalidDataException or Exception, and throw it. You can then catch these more general exceptions at higher levels.
try { // Execute DB call here } catch(SqlException exp) { // Log what you need from here. throw new InvalidOperationException("Data could not be read", exp); }
When you call this method from a higher level, you can just catch an InvalidOperationException. If higher levels require more details, an InnerException will provide a SqlException that can be accessed.
The general approach to handling exceptions that I follow is only to catch what I can usefully work on. It makes no sense to throw a truly general exception to lower levels of code, since you can really expect that everything will go wrong or be able to recover from any exception, for example. OutOfMemoryException or StackOverflowException.
tomasmcguinness
source share