Given this code:
using (var conn = new SqlConnection("..."))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "...";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// ...
}
}
}
}
I'm used to writing try/catch/finally blocks for my data access, however, I'm being exposed to "using" which seems like a much simpler method of doing this. However, I'm trying to figure out how to catch exceptions that may occur.
Could you please give me an example of how you'd catch exceptions?
Edited to add:
I'm being lead to believe that "using" is a replacement for my try/catch/finally blocks. I understand that using doesn't catch exceptions. So how is this a replacement?