2
Answers

Question about exception handling

Dave

Dave

16y
3.1k
1
Hi,

The prevailing wisdom for coding try/catch statements around db connections seems to be as follows:
conn = new MySqlConnection();
cmdGetDate = new MySqlCommand();
try
{
    conn.ConnectionString = connStringSQL;
    conn.Open();

    ...do some stuff   
}
catch (Exception dbErr)
{
    throw new Exception(dbErr.Message);   
}
finally
{
    conn.Close();
    cmdGetDate.Dispose();
    conn.Dispose();
}
My question is, what if an exception occurs on closing the connection (which is inside the finally part of the try/catch)?
Answers (2)