[comp.sys.mac.programmer] Breaking out of a series of routines

tarr-michael@CS.Yale.EDU (michael tarr) (07/09/90)

On an error condition I wish to immediately stop processing a large file
and break out of about 10 routines that have been calling each other and
return to my main event loop. Is there any way to do this without
passing and error flag to each of the calling routines?

tarr@cs.yale.edu

puklich@plains.UUCP (Blayne Puklich) (07/10/90)

tarr-michael@CS.Yale.EDU (michael tarr) wrote in article Breaking out of a
	series of routines:
>On an error condition I wish to immediately stop processing a large file
>and break out of about 10 routines that have been calling each other and
>return to my main event loop. Is there any way to do this without
>passing and error flag to each of the calling routines?

You can use the setjmp() and longjmp() pair of functions to do this, assuming
you're using C.  I'm not very familiar with these, but with a bit of
experimentation and manual reading, you should be able to figure out how
to use these functions.

If you're using Pascal, probably a good way to do this is to pass a return
code back up through each function (make them functions).  Then each and
every call to the functions would be a test for a return code.  This really
isn't as painful as it may seem like.  Pascal function return values are
generally placed in a register (D0 if I remember right), so a test wouldn't
be too time-consuming.

||+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++||
|| Blayne Puklich    puklich@Plains.NoDak.EDU "I think I'm going bald..."  ||
|| NDSU, Fargo, ND   (701) 237-4408            -- Rush, Caress of Steel,   ||
||                                                1975                     ||
||-------------------------------------------------------------------------||

jeremyr@cs.qmw.ac.uk (Jeremy Roussak) (07/10/90)

In article <25526@cs.yale.edu> tarr-michael@CS.Yale.EDU (michael tarr) writes:
>
>On an error condition I wish to immediately stop processing a large file
>and break out of about 10 routines that have been calling each other and
>return to my main event loop. Is there any way to do this without
>passing and error flag to each of the calling routines?

You could use signals, which are described in TechNote 88.  They are
provided for just this purpose.

Jeremy Roussak

mnykanen@cc.helsinki.fi (07/10/90)

In article <5252@plains.UUCP>, puklich@plains.UUCP (Blayne Puklich) writes:
> tarr-michael@CS.Yale.EDU (michael tarr) wrote in article Breaking out of a
> 	series of routines:
>>On an error condition I wish to immediately stop processing a large file
>>and break out of about 10 routines that have been calling each other and
>>return to my main event loop. Is there any way to do this without
>>passing and error flag to each of the calling routines?
.stuff on C's set/longjmp() deleted..
> If you're using Pascal, probably a good way to do this is to pass a return
> code back up through each function (make them functions).  Then each and
> every call to the functions would be a test for a return code.  This really
> isn't as painful as it may seem like.  Pascal function return values are
> generally placed in a register (D0 if I remember right), so a test wouldn't
> be too time-consuming.

In Pascal a GOTO (yuck!) should suffice, at least as defined in the
standard. Since a Pascal GOTO is *not* just a branch instruction (it
even manages the run-time stack frames!) many microcomputer compilers do
not conform, though. But I too recommend functions returning error codes
on aesthetic grounds: they would make error recovery a part of normal
program behaviour, not 'an act of god' miraculously affecting the
control flow.
-- 
Matti Nyk{nen
CS Student at Helsinki U, Finland
email: mnykanen@cc.helsinki.FI

The best opinions available; get them while they're hot!

mxmora@unix.SRI.COM (Matt Mora) (07/10/90)

In article <2745.2699a0d0@cc.helsinki.fi> mnykanen@cc.helsinki.fi writes:
>In article <5252@plains.UUCP>, puklich@plains.UUCP (Blayne Puklich) writes:
>> tarr-michael@CS.Yale.EDU (michael tarr) wrote in article Breaking out of a
>> 	series of routines:
>>>On an error condition I wish to immediately stop processing a large file
>>>and break out of about 10 routines that have been calling each other and
>>>return to my main event loop. Is there any way to do this without
>>>passing and error flag to each of the calling routines?
>.stuff on C's set/longjmp() deleted..
 [more stuff deleted]

>In Pascal a GOTO (yuck!) should suffice, at least as defined in the
>standard. Since a Pascal GOTO is *not* just a branch instruction (it
>even manages the run-time stack frames!) many microcomputer compilers do
>not conform, though. But I too recommend functions returning error codes


I might be wrong but I think Apple has come up with something to do this
already. Its called Signals. (and I know Apple probably didn't come up
with it, maybe its from the unix world) Macintosh Technical Notes #88 describes
the signals mechanism.

I havn't used or studied it very much but it might be what you need.


>Matti Nyk{nen







-- 
___________________________________________________________
Matthew Mora                |   my Mac  Matt_Mora@sri.com
SRI International           |  my unix  mxmora@unix.sri.com
___________________________________________________________

urlichs@smurf.sub.org (Matthias Urlichs) (07/11/90)

In comp.sys.mac.programmer, article <5252@plains.UUCP>,
  puklich@plains.UUCP (Blayne Puklich) writes:
< tarr-michael@CS.Yale.EDU (michael tarr) wrote in article Breaking out of a
< 	series of routines:
< >On an error condition I wish to immediately stop processing a large file
< >and break out of about 10 routines that have been calling each other and
< >return to my main event loop. Is there any way to do this without
< >passing and error flag to each of the calling routines?
< 
< You can use the setjmp() and longjmp() pair of functions to do this, assuming
< you're using C.  I'm not very familiar with these, but with a bit of
< experimentation and manual reading, you should be able to figure out how
< to use these functions.
< 
Using the Signal mechanism (as in MacApp / TechNote 88 / SampleCode XX (I
don't remember right now) works for both C and Pascal, and you can intercept
it in one of the 8 intermediate routines if you have to do some postprocessing
if an error occurs (closing/deleting files, deallocating memory, whatever).

I use it in all my programs -- works great.

-- 
Matthias Urlichs -- urlichs@smurf.sub.org -- urlichs@smurf.ira.uka.de
Humboldtstrasse 7 - 7500 Karlsruhe 1 - FRG -- +49+721+621127(Voice)/621227(PEP)

aries@rhi.hi.is (Reynir Hugason) (07/11/90)

>On an error condition I wish to immediately stop processing a large file
>and break out of about 10 routines that have been calling each other and
>return to my main event loop. Is there any way to do this without
>passing and error flag to each of the calling routines?

Here is an examplery clean way of doing it, assuming you're using Pascal 8)

UNIT FileReader;

INTERFACE
   USES ...

   PROCEDURE ReadFile;
   FUNCTION  Status: INTEGER;

   ... and so on ...

IMPLEMENTATION

   VAR
      errFlag: INTEGER;

   PROCEDURE ReadFile;

      PROCEDURE CheckError;
         BEGIN
            IF (errFlag <> noErr) THEN
               BEGIN

                  { Clean-up now }

                  EXIT(ReadFile);
               END;
         END;

         { Here come your procedures for reading the file }

      BEGIN

         { Initiate sequence }

      END;

   FUNCTION  Status: INTEGER;
      BEGIN
         Status:=errFlag;
      END;
END.

---

Well that's it, now try to do that in C !!! 8)

Mimir (aries@rhi.hi.is) - Aries Software, Inc.

keith@Apple.COM (Keith Rollin) (07/11/90)

In article <25526@cs.yale.edu> tarr-michael@CS.Yale.EDU (michael tarr) writes:
>
>On an error condition I wish to immediately stop processing a large file
>and break out of about 10 routines that have been calling each other and
>return to my main event loop. Is there any way to do this without
>passing and error flag to each of the calling routines?

As Blayne noted, if you are programming in C, you can use
setjmp/longjmp. If you are using MacApp, then you can use the UFailure
routines, CatchFailures and Success. Both of these mechanisms are
subsets of the extensive Signals unit in DTS Sample Code #12. Those
routines can be used from both C and Pascal, and full source code is
provided.

-- 
------------------------------------------------------------------------------
Keith Rollin  ---  Apple Computer, Inc.  ---  Developer Technical Support
INTERNET: keith@apple.com
    UUCP: {decwrl, hoptoad, nsc, sun, amdahl}!apple!keith
"Argue for your Apple, and sure enough, it's yours" - Keith Rollin, Contusions