473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Exiting A Multi-level Procedure From the Lowest Level

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
4 1725
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Robbie Beane | last post by:
Is there any way to stop execution of a function without killing the whole script? Is there a command that will jump out of a function back to the main script, or calling function?
2
by: Hal Vaughan | last post by:
I have a class that is sometimes called from the command line and sometimes from another class. When the user clicks "Quit" on the panel it opens, if it is running from the command line, it...
22
by: L. Westmeier | last post by:
Is there a way to have to exiting point in a void function? I don't want to exit the program but just this function. Any answers appreciated. L. Westmeier
8
by: Andrew Warren | last post by:
I'm trying to exit a Windows Forms application while in the form's constructor (after InitializeComponent() has been called) and am finding that calling Application.Exit () still leaves the form...
3
by: Bonj | last post by:
When trying to debug an ASP.NET project by placing a breakpoint in one of the functions in the codebehind, VS correctly stops at the breakpoint, and I can step through. But soon after, the process...
5
by: Tim Werth | last post by:
I have a .NET console application that is kicked off by a .NET Windows service. They communicate via .NET Remoting, although there isn't much communication between the two while the console app is...
1
by: kuhrty | last post by:
Hi, I am creating a multi-winform project. Everything has been working fine, except after I perform an update to the database and decide to Exit the winform after the update operation is...
6
by: Ant | last post by:
Hi all, I'm putting together a simple help module for my applications, using html files stored in the application directory somewhere. Basically it sets up a basic web server, and then uses the...
2
by: jrpfinch | last post by:
Is the following the most elegant way to exit a multi-threaded application on a Ctrl-C? I am a complete beginner and would have thought there was some way of doing it without having to use while...
23
by: Boltar | last post by:
Hi I'm writing a threading class using posix threads on unix with each thread being run by an object instance. One thing I'm not sure about is , if I do the following: myclass::~myclass() {...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.