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!
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With