Results 1 to 7 of 7

Thread: [RESOLVED] Structured Error Handling question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Florida
    Posts
    107

    Resolved [RESOLVED] Structured Error Handling question

    I did an exercise and lesson about structured error handling yesterday.

    I don't see the point in the "Finally" clause. Wouldn't code placed after the "End Try" do the same thing, or did I miss something?

    Tom

  2. #2
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Structured Error Handling question

    The code in the finally part gets run whether the code errors or not.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Structured Error Handling question

    As Merrion says, the contents of the Finally block gets executed under all circumstances. Have a look at this example:
    VB Code:
    1. Try
    2.     'Open database connection.
    3.  
    4.     'Retrieve data.
    5. Catch
    6.     'Notify user of error.
    7.  
    8.     'Exit method.
    9. Finally
    10.     'Close database connection.
    11. End Try
    In this case, if you didn't have a Finally block and an exception is thrown then the database connection wouldn't be closed. Finally is usually used for things like closing files, connections, etc. that must be done regardless of the circumstances. The Finally block will still be executed even if a Return statement has been executed, or even if the form or application has been closed. There is no other way to ensure that.
    Last edited by jmcilhinney; Apr 9th, 2006 at 07:56 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Structured Error Handling question

    Quote Originally Posted by jmcilhinney
    The Finally block will still be executed even if a Return statement has been executed, or even if the form or application has been closed. There is no other way to ensure that.
    THANK YOU! I've been trying to get SOMEONE to tell me the reason for the finaly, and that makes sense. This is the first time anyone has been able to explain that well enough that I get it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Florida
    Posts
    107

    Re: Structured Error Handling question

    The Finally block will still be executed even if a Return statement has been executed, or even if the form or application has been closed. There is no other way to ensure that.
    Thanks - the lesson I have didn't make that point at all. - Tom

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Florida
    Posts
    107

    Re: Structured Error Handling question

    Where is Royston Vasey?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Structured Error Handling question

    Quote Originally Posted by Tom951
    Where is Royston Vasey?
    It's the fictional town in which the English comedy series "The League of Gentlemen" is set. Those who've seen it should also understand the "Egregious" reference in my profile. Those who haven't seen it should.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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