3

I've been implementing TRY-CATCH relative to USING like the following example:

private void someDatabaseMethod(string userName) {

    try {

        using(var conn = new SqlConnection(connString))
        using(var comm = new SqlCommand()) {
            comm.Connection = conn;
            comm.CommandType = CommandType.Text;
            comm.CommandText = string.Concat(@"SELECT UserID FROM xxx WHERE UserName = '", userName,@"'");
            conn.Open();
            object x = comm.ExecuteScalar();
            UserID = (x==null)? 0: (int)x;
        }
    } catch(Exception) {
        throw;
    }
}

I've just seen this MSDN EXAMPLE which seems to point towards the TRY-CATCH being within the USING. So my example would look like the following:

private void someDatabaseMethod(string userName) {

        using(var conn = new SqlConnection(connString))
        using(var comm = new SqlCommand()) {
            comm.Connection = conn;
            comm.CommandType = CommandType.Text;
            comm.CommandText = string.Concat(@"SELECT UserID FROM xxx WHERE UserName = '", userName,@"'");

            try {

               conn.Open();
               object x = comm.ExecuteScalar();
               UserID = (x==null)? 0: (int)x;

            } catch(Exception) {
               throw;
            }
        }
}

Is this a more efficient layout? If so, why?


EXTRA ADDITIONAL NOTE

The reason for the TRY-CATCH is to re-throw the exception so that I bubble it up to the next level - so I'd like to have a CATCH somewhere in the code.

whytheq
  • 34,466
  • 65
  • 172
  • 267

5 Answers5

3

It depends on your goals. If you want to do something with command or connection in catch block, then it should be within using.

alex
  • 12,464
  • 3
  • 46
  • 67
2

If you just throw the catched exception, the try-catch block isn't necessary at all. The using will dispose the conenction and command properly.

laszlokiss88
  • 4,001
  • 3
  • 20
  • 26
  • The reason for the `TRY-CATCH` is to re-throw the exception so that I can bubble it up to the next level - so I'd like to have a `CATCH` somewhere in the code. – whytheq Apr 11 '13 at 14:47
2

TRY-CATCH I use in using only if I want to LOG Exception or it I have transaction - to rollback it in except block. using is translated by compiler in TRY-FINALLY - you can check it with IL Disassembler (ildasm.exe) or reflector to release your disposable resources. so that using is equivalent to :

try 
{
 //do job
} 
finally
{
  Resource.Dispose()
}
ALZ
  • 1,997
  • 2
  • 27
  • 44
2

Second one is more efficient. For first one; you can not access connection object from catch block, and can not close it. Also if you was using a transaction over this connection, you could not rollback the transaction when any error occurs...

Kenan Kocaerkek
  • 319
  • 3
  • 9
  • +1 for first one - if it errors on `conn.Open();` what happens first: will the using deal with the problem and free resources ? or will it go to CATCH before using has a chance to garbage collect? – whytheq Apr 11 '13 at 14:50
0
  • Don't catch exceptions you cannot handle at this place.
  • catch{throw;} is of no use except adding complexity
  • catch and handle exceptions as near at the exception source as you are able to handle them

Read extensive discussion here

Community
  • 1
  • 1
  • 1
    catch{throw;} bubbles the exception up to a higher level and it is dealt with there – whytheq Apr 11 '13 at 14:48
  • 1
    And therefore catch-throw is for nothing as the exception will "bubble" up also without this construct. –  Apr 11 '13 at 15:51