By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,099 Members | 2,156 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,099 IT Pros & Developers. It's quick & easy.

Using GoTo or Resume

P: n/a
In one of my error handler exit routines I have the following

Exit_cmdSend_Click:
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing

In two places within my sub procedure I want to exit the sub. Instead
of repeating the code shown above, I want to jump straight to the exit
routine. I know it is not wise to use GoTo unless handling errors. Is
it okay to use it in this case, as in

GoTo Exit_cmdSend_Click

or can I use Resume, as in

Resume Exit_cmdSend_Click

or is there a better alternative?
Marcus
******

Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
The most common way to handle this is to put the clean-up code just before an
Exit Function or Exit Sub, put the error handling label below that, and have
the error handler resume to a label just before the clean-up code. This works
well enough, and you'll see a lot of perfectly good example code written in
this style.

Also, I see that you are using ADO which is not supposed to have the same
problem in VB as DAO where resources can leak if objects are not explicitly
cleaned up. Personally, I choose not to trust that.

What I like to do is get the clean-up code out of the procedure altogether so
it isn't duplicated in multiple procedures. I do this by making wrapper
classes for recordsets and connections or databases (depending whether using
DAO or ADO), and making the recordset wrappers hold a reference to the
database wrappers. That way, the database wrapper can't go out of scope
before all the recordset wrappers do. This scheme works regardless of whether
a procedure exits normally, exits via an internal error handler, or exits to a
calling procedure's error handler, and doesn't add junk that obscures each
procedure's basic function.

On 30 Apr 2005 20:14:42 -0700, "Marcus" <to*******@yahoo.ca> wrote:
In one of my error handler exit routines I have the following

Exit_cmdSend_Click:
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing

In two places within my sub procedure I want to exit the sub. Instead
of repeating the code shown above, I want to jump straight to the exit
routine. I know it is not wise to use GoTo unless handling errors. Is
it okay to use it in this case, as in

GoTo Exit_cmdSend_Click

or can I use Resume, as in

Resume Exit_cmdSend_Click

or is there a better alternative?
Marcus
******


Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.