473,799 Members | 3,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1740
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:::mgf0 0 <at> earthlink <decimal-point> net
Oakland, CA (USA)

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

iQA/AwUBQtf17IechKq OuFEgEQLbogCfRp yiPJiLvRHaT1Q/u16m11QSAS0AnRh l
a+fmC+DM8Kq8qJT rOPGCjBvc
=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:::mgf0 0 <at> earthlink <decimal-point> net
Oakland, CA (USA)

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

iQA/AwUBQtf17IechKq OuFEgEQLbogCfRp yiPJiLvRHaT1Q/u16m11QSAS0AnRh l
a+fmC+DM8Kq8qJT rOPGCjBvc
=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
3157
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
3046
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 quits. The problem is that I need a way to terminate the class and the object without using quit, so it doesn't kill other classes. The QUIT button calls an actionListener, so if I have "return" in the listener, it closes the window, but the class is...
22
5999
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
2600
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 displayed and running. Trying to force the form to close with this.Close() does not work either. Any suggestions? Thanks.
3
1516
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 stops dead and can't be stepped through anymore (even though there is no error) - I'm guessing that the aspnet_wp.exe is detecting that if it's inactive for a period of n seconds or so, then it exits until it gets another request. Is there...
5
3864
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 running. There can be, and are typically, several instances of the console app running at the same time communicating back to the service. The console app creates a delegate to do some work while the main thread waits on a ManualResetEvent to...
1
1915
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 complete. I have error handling in my code but not on the dispose method. I call the dispose method exiting the application using the code below but results in this error message "An unhandled exception of type 'System.NullReferenceException'
6
2519
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 webbrowser module to display it in the users browser. I have it set up to call sys.exit(0) if the url quit.html is called, but for some reason (on Windows XP via the cmd.exe shell) python won't let me have the console back. I get the System Exit...
2
2508
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 1: pass, but have yet to find a way. N.B. exit() is a method for cleanly exiting the thread using a queue. Many thanks Jon
23
5719
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() { : : do stuff
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.