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

Exiting A Multi-level Procedure From the Lowest Level

P: n/a
If I'm in, say, a 3rd level sub and want to exit, not only the sub but
the entire procedure, without a GoTo, can this be done without using
switches?

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


P: n/a
SH********@aol.com wrote:
If I'm in, say, a 3rd level sub and want to exit, not only the sub but
the entire procedure, without a GoTo, can this be done without using
switches?


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Huh? I'm assuming you mean that you have a subroutine that has been
called like this:

Sub 1 calls-> Sub 2 calls-> Sub 3

And you want to exit Sub 3 back to Sub 1 without doing anything in Sub
2. There is no way to go directly back to Sub 1. You have to Exit Sub
3 w/ some kinda of flag set that indicates to Sub 2 that Sub 3 is ending
in a defined state. Sub 2 checks that flag and if it means something
like "stop all procedures in Sub 2" then Sub 2 immediately Exits back to
Sub 1. Sub 2 can set another flag, or just return the same flag that
Sub 3 set, that "tells" Sub 1 that Sub 2 stopped because of the flag
received from Sub 3.

What I do is set up error traps in Sub 1 and no error traps in Sub 2 &
Sub 3. Then if something happens in Sub 3 that makes me want to return
to Sub 1 w/o any interference by the intervening Subs I use Err.Raise in
Sub 3. This will cause a cascade back thru all intervening Subs until
it hits a Sub that has an error trap. Then that Sub does whatever is
necessary when that user-defined error appears. E.g.:
Private Sub Sub1()

On Error GoTo err_

' call Subs
Sub2

exit_:
Exit Sub

err_:
If Err.Number = vbObjectErr+513 Then
' Handle the Sub 3 state exit code
Else
' usual error handling code
End If

End Sub

Private Sub Sub2()

' call Sub 3
Sub3

End Sub

Private Sub Sub3()

' does whatever ....

' Here it checks to see if it needs to jump back to Sub 1
If <some condition> = True Then
Err.Raise vbObjectError + 513 'the number indicates the Sub's state
End If

End Sub

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQtf17IechKqOuFEgEQLbogCfRpyiPJiLvRHaT1Q/u16m11QSAS0AnRhl
a+fmC+DM8Kq8qJTrOPGCjBvc
=qR/v
-----END PGP SIGNATURE-----
Nov 13 '05 #2

P: n/a
SH********@aol.com wrote:
If I'm in, say, a 3rd level sub and want to exit, not only the sub but
the entire procedure, without a GoTo, can this be done without using
switches?


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Huh? I'm assuming you mean that you have a subroutine that has been
called like this:

Sub 1 calls-> Sub 2 calls-> Sub 3

And you want to exit Sub 3 back to Sub 1 without doing anything in Sub
2. There is no way to go directly back to Sub 1. You have to Exit Sub
3 w/ some kinda of flag set that indicates to Sub 2 that Sub 3 is ending
in a defined state. Sub 2 checks that flag and if it means something
like "stop all procedures in Sub 2" then Sub 2 immediately Exits back to
Sub 1. Sub 2 can set another flag, or just return the same flag that
Sub 3 set, that "tells" Sub 1 that Sub 2 stopped because of the flag
received from Sub 3.

What I do is set up error traps in Sub 1 and no error traps in Sub 2 &
Sub 3. Then if something happens in Sub 3 that makes me want to return
to Sub 1 w/o any interference by the intervening Subs I use Err.Raise in
Sub 3. This will cause a cascade back thru all intervening Subs until
it hits a Sub that has an error trap. Then that Sub does whatever is
necessary when that user-defined error appears. E.g.:
Private Sub Sub1()

On Error GoTo err_

' call Subs
Sub2

exit_:
Exit Sub

err_:
If Err.Number = vbObjectErr+513 Then
' Handle the Sub 3 state exit code
Else
' usual error handling code
End If

End Sub

Private Sub Sub2()

' call Sub 3
Sub3

End Sub

Private Sub Sub3()

' does whatever ....

' Here it checks to see if it needs to jump back to Sub 1
If <some condition> = True Then
Err.Raise vbObjectError + 513 'the number indicates the Sub's state
End If

End Sub

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQtf17IechKqOuFEgEQLbogCfRpyiPJiLvRHaT1Q/u16m11QSAS0AnRhl
a+fmC+DM8Kq8qJTrOPGCjBvc
=qR/v
-----END PGP SIGNATURE-----
Nov 13 '05 #3

P: n/a
In the scenario of Sub1 calls Sub2 which calls Sub3, I would like to
abort the procedure while in sub3 and not do anything such as return to
Sub1.

Nov 13 '05 #4

P: n/a
On 19 Jul 2005 06:06:28 -0700, "SH********@aol.com" <SH********@aol.com>
wrote:
In the scenario of Sub1 calls Sub2 which calls Sub3, I would like to
abort the procedure while in sub3 and not do anything such as return to
Sub1.


The only way you could do it is by raising an exception. You can fake it by
checking return flags. Why do you want to do this? It would really help to
know the context.

In general, the idea of a procedure returning to a specific caller's caller
seems like pathological code coupling to me. The procedure must make
assumptions about what other code called it which is something you usually
want to avoid.
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.