About FINALLY clause

Discussion in 'ASP .Net' started by Henri, Dec 14, 2004.

  1. Henri

    Henri Guest

    This question is applicable to all the languages, but could someone provide
    me with an code example where FINALLY is really necessary.
    FINALLY is often used for this kind of code:

    Try
    ....
    open a file
    read some date
    .....
    Catch
    ... dealing with the file error ....
    Finally
    close the file
    End Try

    But wouldn't the following (without FINALLY) work as well?

    Try
    ....
    open a file
    read some date
    .....
    Catch
    ... dealing with the file error ....
    End Try
    close the file

    Please tell me what I'm missing.
    Thanks
    Henri
    Henri, Dec 14, 2004
    #1
    1. Advertising

  2. Henri

    Karl Seguin Guest

    NO!

    What happens if you can't "deal" with the file..say because the exception
    was OutOfMemory (not what you thought it was going to be eh?) you should
    still do what you can to close the file. Also, what happens in your
    "dealing" with the exception that you decide to throw it...your close call
    will never get called. The point of finally is that you are [almost]
    guaranteed that the code will get called. Putting your cleanup AFTER the
    try/catch doesn't provide a guarantee. Here's another case....your catch
    code generates an exception...your cleanup will never get called.

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Henri" <> wrote in message
    news:...
    > This question is applicable to all the languages, but could someone

    provide
    > me with an code example where FINALLY is really necessary.
    > FINALLY is often used for this kind of code:
    >
    > Try
    > ....
    > open a file
    > read some date
    > ....
    > Catch
    > ... dealing with the file error ....
    > Finally
    > close the file
    > End Try
    >
    > But wouldn't the following (without FINALLY) work as well?
    >
    > Try
    > ....
    > open a file
    > read some date
    > ....
    > Catch
    > ... dealing with the file error ....
    > End Try
    > close the file
    >
    > Please tell me what I'm missing.
    > Thanks
    > Henri
    >
    >
    >
    Karl Seguin, Dec 14, 2004
    #2
    1. Advertising

  3. Try
    ....
    open a file
    read some date
    .....
    Catch
    ... dealing with the file error ....
    .... exit sub or rethrow error ...
    Finally
    close the file
    End Try

    In the above code the exit sub or rethrow error would normally exit the
    function, and if an error occurs within the Catch block the function would
    also be exited. Anything after the try block would not be executed.
    However, anything within the Finally block will be executed before the
    function is exited. That is why it is useful.

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net



    "Henri" <> wrote in message
    news:...
    > This question is applicable to all the languages, but could someone
    > provide
    > me with an code example where FINALLY is really necessary.
    > FINALLY is often used for this kind of code:
    >
    > Try
    > ....
    > open a file
    > read some date
    > ....
    > Catch
    > ... dealing with the file error ....
    > Finally
    > close the file
    > End Try
    >
    > But wouldn't the following (without FINALLY) work as well?
    >
    > Try
    > ....
    > open a file
    > read some date
    > ....
    > Catch
    > ... dealing with the file error ....
    > End Try
    > close the file
    >
    > Please tell me what I'm missing.
    > Thanks
    > Henri
    >
    >
    >
    Steve C. Orr [MVP, MCSD], Dec 14, 2004
    #3
  4. Henri

    DKode Guest

    I believe if you throw an exception from your catch clause, it will not
    execute code outside of your try-catch clause if you throw an exception
    up the stack. Any code you put inside of finally will run whether you
    throw exceptions or not regardless.

    anyone correct me if i am wrong.
    DKode, Dec 14, 2004
    #4
  5. Henri

    Henri Guest

    Thanks to all for your answers !
    FINALLY I could understand the use of FINALLY ;-)

    Henri

    "Henri" <> a écrit dans le message de
    news:...
    > This question is applicable to all the languages, but could someone

    provide
    > me with an code example where FINALLY is really necessary.
    > FINALLY is often used for this kind of code:
    >
    > Try
    > ....
    > open a file
    > read some date
    > ....
    > Catch
    > ... dealing with the file error ....
    > Finally
    > close the file
    > End Try
    >
    > But wouldn't the following (without FINALLY) work as well?
    >
    > Try
    > ....
    > open a file
    > read some date
    > ....
    > Catch
    > ... dealing with the file error ....
    > End Try
    > close the file
    >
    > Please tell me what I'm missing.
    > Thanks
    > Henri
    >
    >
    >
    >
    Henri, Dec 14, 2004
    #5
    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. Lee Simpson

    Re: Formatting string in WHERE clause

    Lee Simpson, Jul 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,260
    Lee Simpson
    Jul 18, 2003
  2. Jan
    Replies:
    2
    Views:
    1,428
    Mike Treseler
    Dec 16, 2004
  3. bxbxb3

    "else process" clause

    bxbxb3, May 26, 2005, in forum: VHDL
    Replies:
    3
    Views:
    661
    combinational.logic $ soc-ip.com
    May 27, 2005
  4. crazyrdx

    use clause

    crazyrdx, Aug 30, 2005, in forum: VHDL
    Replies:
    1
    Views:
    620
  5. David Lozzi

    Try...Catch...Finally not firing finally?

    David Lozzi, Apr 23, 2007, in forum: ASP .Net
    Replies:
    12
    Views:
    794
    Alvin Bruney [MVP]
    May 11, 2007
Loading...

Share This Page