Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm using SqlConnection, SqlDataReader, SqlCommand and SqlParameters for communcation with my database. I'm looking for advise about catching the most relevant Exceptions instead of throwing the a new Exception(), it's a too broad class.

All help is appreciated!

share|improve this question
up vote 7 down vote accepted

The most relevant likely equates to the most common environmental conditions that can happen. Coding errors will be fixed but you can't control the environmental issues.

Also, besides the docs on the classes, you can simply try those conditions and make sure you're handling the condition properly. Years ago, a developer asked me what exception would be thrown in some various technology if connectivity was lost - so I pulled his network cable and we ran his code :)

Some common ones would be:

  1. Unplug your network cable
  2. Disable your network adapter
  3. Stop SQL Server
  4. Create a sproc that takes a very long time
  5. Let the database/tempdb get full
  6. Ensure you don't have permissions to the sql server
  7. Create a sproc that deadlocks

In some of these cases (network, timeout, deadlock victim), you'll want to retry perhaps with increasing waits. For others like db full the best you can hope for is to provide good guidance or even log details for the admin.

By trying the conditions you're assured what exception gets thrown and you can verify you're handling the condition appropriately.

share|improve this answer
    
Excellent example of "teaching a man to fish". – DarkBobG Dec 3 '11 at 14:08
    
@bryanmac - Thanks for your answer. I had only coding errors in mind when I asked the question and I did not think of environmental conditions, thank you! – Erik Larsson Dec 3 '11 at 14:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.