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
Printable View
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
The code in the finally part gets run whether the code errors or not.
As Merrion says, the contents of the Finally block gets executed under all circumstances. Have a look at this example: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.VB Code:
Try 'Open database connection. 'Retrieve data. Catch 'Notify user of error. 'Exit method. Finally 'Close database connection. End Try
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.Quote:
Originally Posted by jmcilhinney
-tg
Thanks - the lesson I have didn't make that point at all. - TomQuote:
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.
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. :)Quote:
Originally Posted by Tom951