0

The following 2 sets of codes produce same results, but I'm wondering which of these is the correct way of wrapping with the try...catch block. What's the difference between the 2 of them?

Edited: Which of this will properly capture the error, and also to make sure the connection will be closed even if there is an exception.

1

try
{
    using (SqlConnection conn = new SqlConnection(connString))
    {
        SqlCommand cmd = new SqlCommand("Drop Table sometable", conn);
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
    }
}
catch
{
    //
}

2

using (SqlConnection conn = new SqlConnection(connString))
{
    try
    {
        SqlCommand cmd = new SqlCommand("Drop Table sometable", conn);
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
    }
    catch
    {
        //
    }
}
Mo Patel
  • 2,321
  • 4
  • 22
  • 37
C.J.
  • 3,409
  • 8
  • 34
  • 51

5 Answers5

3

The difference is that in the #2 SqlConnection conn object will be disposed after the catch block will be finished. In #1 your connection will be disposed and than you will end up in the catch block.

milagvoniduak
  • 3,214
  • 1
  • 18
  • 18
2

Both are fine, depended on if you want to catch an error when opening the connection, and then of course you need to close the connection when you are done with it. usually closing earlier is better.

Mothware
  • 312
  • 2
  • 10
1

the difference is:

if you use code 2, and your connString you use in your using statement is invalid, it will throw an exception, because it is not in the try catch block.

in code 1, it will just catch that exception. but you will not be able to use your SqlConnection in the catch block

JoJo
  • 806
  • 1
  • 13
  • 26
1

I thik that you are aware of exception handling. We write all of our codes inside the catch block that can arise suspicious/unknown behavior to our applications. In that case the catch block handles the suspicious behavior of our application. In context to your above two block of codes .... For the first one- any exception caused by any one statement of your codes will be handled by catch block. But for second one- exception caused by "using (SqlConnection conn = new SqlConnection(connString))" statement cannot be handled by your catch block, because it is outside of the try block. To test this just place some false connection string in sqlconnection.

Hope this will gives you better understanding for your scenario.

Ravindra Kumar
  • 601
  • 1
  • 5
  • 13
0

The using statement is a syntactic shortcut to represent a try/catch/finally. You could right your code as below, and would have the same effect as your examples above.

        SqlConnection conn
        try
        {
            conn = new SqlConnection(connString)
            SqlCommand cmd = new SqlCommand("Drop Table sometable", conn);
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
        }
        catch
        {
            //
        }
        finally
        {
           if(conn != null)
           {
                conn.Dispose()
           }
       }
Keith
  • 1,119
  • 2
  • 12
  • 23