none
using with try-catch or try-catch-finally ? RRS feed

  • Question

  • What do you use? using statetment or try-finallly ?

    With the using statement... what is the best way to catch a exception?

    Now, i'm using a try-catch-finally for the connection and using for the commands, dataadapters, datareaders ... etc.., but i don't know if is the best way.

    Regards.
    Thursday, February 15, 2007 9:20 AM

Answers

  • Hi,

    To my opinion, when using connections and stuff, I still prefer the try catch blocks and dispose everything in the finally block. How can you otherwise get detailed error information since using is only for disposing objects? The using statement is good if you have a single object that needs to be disposed after using it.

    I think it has a lot to do with personal preference.

    Greetz,

    Geert

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

    Thursday, February 15, 2007 9:52 AM
  • Well, if you are considering "using" statement, it does not takes care of your exceptions etc. It disposes your object (as it implements IDisposable) while coming out of the "using" block. So you might like to have "using" block inside your try-catch. By doing so, you can have exception information and "using" statement will take care of your object disposal. Otherwise you can always explicitly dispose objects in finally.

    Hope this helps.

    Thursday, February 15, 2007 10:15 AM
  • As per my knowledge, yes. It disposes the object when it comes out of "using" statement. And it might be coming out because of exception.
    Thursday, February 15, 2007 11:20 AM
  • When disposing a connection it automatically closes the connection. You can verify this with a tool called reflector that shows you the code from .NET.

    Greetz,

    Geert

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

    Thursday, February 15, 2007 12:00 PM
  • I prefer the using statement (one of the nicest feature of C#). The exceptions to this rule are generally:
    - when I want to do something useful in the catch bloch (most of the time you don't have anything useful to do, so just leave the exception to propagate up the call stack)
    - when I must do something else in the finally block beside disposing the resource.

    A using statement inside a try/catch is unnecessary. And it looks weird, if you consider that using is only a language shortcut which is translated to a try/finally. If you really need a catch - just use try-catch-finally, not try-using-catch.

     

    Thursday, February 15, 2007 12:18 PM

All replies

  • Hi,

    To my opinion, when using connections and stuff, I still prefer the try catch blocks and dispose everything in the finally block. How can you otherwise get detailed error information since using is only for disposing objects? The using statement is good if you have a single object that needs to be disposed after using it.

    I think it has a lot to do with personal preference.

    Greetz,

    Geert

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

    Thursday, February 15, 2007 9:52 AM
  • Well, if you are considering "using" statement, it does not takes care of your exceptions etc. It disposes your object (as it implements IDisposable) while coming out of the "using" block. So you might like to have "using" block inside your try-catch. By doing so, you can have exception information and "using" statement will take care of your object disposal. Otherwise you can always explicitly dispose objects in finally.

    Hope this helps.

    Thursday, February 15, 2007 10:15 AM
  • Aham, but ... if a exception is throwed inside the using block ... the objects is disposed too ??

    Regards.
    Thursday, February 15, 2007 11:11 AM
  • As per my knowledge, yes. It disposes the object when it comes out of "using" statement. And it might be coming out because of exception.
    Thursday, February 15, 2007 11:20 AM
  • Ok, this was my fear.

    Thanks very much to both for your replies ;)

    Regards.
    Thursday, February 15, 2007 11:29 AM
  • Some question more ....

    For example:



    using(OracleConnection oc = new OracleConnection(myConnectionString))
    {
           // Do stuff here ...
    }

     


    This estructure ... will close besides dispose the connection?

    Regards.
    Thursday, February 15, 2007 11:51 AM
  • When disposing a connection it automatically closes the connection. You can verify this with a tool called reflector that shows you the code from .NET.

    Greetz,

    Geert

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

    Thursday, February 15, 2007 12:00 PM
  • I prefer the using statement (one of the nicest feature of C#). The exceptions to this rule are generally:
    - when I want to do something useful in the catch bloch (most of the time you don't have anything useful to do, so just leave the exception to propagate up the call stack)
    - when I must do something else in the finally block beside disposing the resource.

    A using statement inside a try/catch is unnecessary. And it looks weird, if you consider that using is only a language shortcut which is translated to a try/finally. If you really need a catch - just use try-catch-finally, not try-using-catch.

     

    Thursday, February 15, 2007 12:18 PM
  • OK!

    Thansk to both for yours usefull answers again, really.

    Regards.
    Thursday, February 15, 2007 4:11 PM