Using or TryCatchFinally

Discussion in 'ASP .Net' started by K Viltersten, Oct 24, 2009.

  1. K Viltersten

    K Viltersten Guest

    I can't decide whether i should deploy the
    using statement or Try-Catch-Finally
    statement.

    I'd prefer using because it's more compact
    and automagically disposes stuff.

    On the other hand, i can't be sure that the
    DB is working, so i might end up with an
    ugly exception, which requires an explicit
    try/catch to handle...

    Suggestions?

    --

    Regards
    Konrad Viltersten
    --------------------------------
    May all spammers die an agonizing death;
    have no burial places; their souls be
    chased by demons in Gehenna from one room
    to another for all eternity and beyond.
    K Viltersten, Oct 24, 2009
    #1
    1. Advertising

  2. K Viltersten wrote:
    > I can't decide whether i should deploy the
    > using statement or Try-Catch-Finally statement.
    >
    > I'd prefer using because it's more compact
    > and automagically disposes stuff.
    >
    > On the other hand, i can't be sure that the DB is working, so i might
    > end up with an ugly exception, which requires an explicit try/catch to
    > handle...
    >
    > Suggestions?


    IDisposable resource = ...;
    using (resource) {
    ...
    }

    is functionally equivalent to

    IDisposable resource = ...;
    try {
    ...
    } finally {
    if (resource != null)
    resource.Dispose ();
    }

    (The compiler creates a temp variable to ensure that resource is not
    changing.)

    As you can see there is no catch block in the using statement
    implementation.

    So if you need a catch block then you have to create a catch block even
    if you choose the using statement over a try-finally block.

    Marvin
    Marvin Landman, Oct 24, 2009
    #2
    1. Advertising

  3. K Viltersten

    K Viltersten Guest

    SV: Using or TryCatchFinally

    <snip>
    > So if you need a catch block then you have to create
    > a catch block even if you choose the using statement
    > over a try-finally block.


    Dang... :)

    Thanks to both of you.

    --

    Regards
    Konrad Viltersten
    --------------------------------
    May all spammers die an agonizing death;
    have no burial places; their souls be
    chased by demons in Gehenna from one room
    to another for all eternity and beyond.
    K Viltersten, Oct 24, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William F. Robertson, Jr.
    Replies:
    1
    Views:
    969
    Kathy Burke
    Jun 25, 2003
  2. pete
    Replies:
    1
    Views:
    2,933
    Jon Yates
    Aug 29, 2003
  3. timmso
    Replies:
    1
    Views:
    477
    Alex Papadimoulis
    Dec 12, 2003
  4. Replies:
    2
    Views:
    544
  5. Manavendra Gupta
    Replies:
    1
    Views:
    356
    Manavendra Gupta
    Mar 7, 2004
Loading...

Share This Page