try catch finally to close and dispose, but still wantApplication_error to fire

Discussion in 'ASP .Net' started by jobs, Nov 16, 2007.

  1. jobs

    jobs Guest

    re: try catch finally to close and dispose, but still want
    Application_error to fire

    1. If catch an exception to to dispose and close of a ado connect, how
    can I allow the exception to still trigger my application_error event?

    2. And/Or, Is there any way to close and dispose of connections left
    behind by methods that might have failed without closing connections
    in the application_error event?
     
    jobs, Nov 16, 2007
    #1
    1. Advertisements

  2. jobs

    Guest Guest

    Sorry,

    I'm not sure if I have missed something from the picture, but I would
    say... try --> catch --> throw

    Inside catch if you throw the exception it will be bubbled up again.

    HTH
    Braulio
    --
    /// ------------------------------
    /// Braulio Diez
    ///
    /// http://www.tipsdotnet.com
    /// ------------------------------
     
    Guest, Nov 16, 2007
    #2
    1. Advertisements

  3. jobs

    bruce barker Guest

    use try/finally without a catch, or c# using statement.

    -- bruce (sqlwork.com)
     
    bruce barker, Nov 16, 2007
    #3
  4. Use finally to close/dispose of connections. The Finally block always
    executes.
    If your purpose in creating a catch block is to simply close/dispose of
    stuff before re-throwing the error then simply don't bother with the catch
    block at all. The whole point of the finally block is to perform tidy up
    code that must occur whether or not an exception is being thrown.
     
    Anthony Jones, Nov 16, 2007
    #4
  5. jobs

    jobs Guest

    Thanks, But I do still need to make sure I close/dispose when an error
    occurs. Can I do that in Appliation_error sub of the Global.asax? If
    so, how? If not I will have to rethrow the same exception? how?
     
    jobs, Nov 16, 2007
    #5
  6. Let me emphasis what I've already said:-

    "The Finally block always executes."

    That means even if the flow of code is now in an error condition the code in
    the finally block will still execute. Thats why its there. If code in the
    finally block didn't execute when an error occured it wouldn't have any
    point in being there since it would be no different than putting code at the
    bottom of the try block.

    So its in the finally block that you would place close/dispose code.

    C# provides the using() alternative which allows you to define a block of
    code that uses a disposable object that must always be disposed when
    execution leaves block. The manner of leaving the block is immaterial,
    either by normal code execution or due to an exception.
     
    Anthony Jones, Nov 17, 2007
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.