Results 1 to 5 of 5

Thread: [02/03] How final is Finally?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    [02/03] How final is Finally?

    Just a quick question, would code in the Finally block of a Try statemnet get executed if I use Exit Sub in the Catch block? MSDN states "absolutely, by all means, always" but not explicitly about Exit Sub and I am kind of sceptical.

    To reproduce the case just to check it would take some time so I decided to ask here if someone knows.

    Thanks
    VB 2005, Win Xp Pro sp2

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: [02/03] How final is Finally?

    Well, you could always just run a small test, as I just did. But seeing as how I've already done it, I'll tell you.

    The Finally block gets run through always, regardless of a Return or Exit statement.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [02/03] How final is Finally?

    Yeah, I played around with this a few months back, and posted a thread on it. I found it a bit odd that a return statement anywhere prior to the Finally block was actually hitting the finally block. It appears that when you implement a Try....Catch block (whether there is a finally, or not), the behavior of the Try block is much like a function in itself. The Exit Sub, or Return statement will exit the code you are in, but run any Finally block, before exiting the outer sub.

    I can understand why this was done, but it is kind of odd. When the Finally block is hit, the variables in the sub/function can be in an undeterminate state (some might not be initialized yet, or perhaps they have been). An early exit in a Try block may not be an early exit at all.
    My usual boring signature: Nothing

  4. #4
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: [02/03] How final is Finally?

    One of the reasons to do so was to relaes recources even if ther was a little mishap... like files might were opened for reading should be released.
    and in VB.Net you can determine if a variable was or wasn't initialized.
    so that wouldn't be a prob neither.
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [02/03] How final is Finally?

    Yes, you can determine whether a variable was or wasn't initialized, but if you forget to, and it wasn't, then your Finally block will throw, which is often not what you expect.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width