Put your code... (was Re: gotos

William E. Sommerfeld wesommer at athena.mit.edu
Thu Apr 28 13:46:23 AEST 1988


In article <2030 at optilink.UUCP> cramer at optilink.UUCP (Clayton Cramer) writes:
>Since the objective is do some cleanup (close the editor's files, spray
>error messages on the user's screen, post an article about the failure
>on USENET, etc.) before giving up, the correct solution is;
>
>if (failure)
>    ErrorHandler(__FILE__, __LINE__);
>
 [error handler prints a message on stdout and exits]
>
>Note that this gives the catastrophic failure cleanup the poster was saying
>was needed before killing the process, without the evil goto.  

This assumes that the only way to recover is to kill off the process
completely.  In UNIX, where each user-typed command starts a new
process, this almost makes sense, since it returns you to the shell. 

However, there are environments where killing off the process in
response to an error is not acceptable--editors, shells, or network
server processes come to mind here, as does the kernel.  You want to
be able to gracefully unwind the stack, freeing up resources and
unlocking locks as you go.  People familiar with PL/I's 'on cleanup'
and LISP's 'unwind-protect' should understand what I mean here...  At
least there are some approximations to this under UNIX... both Amdahl
and Apollo have implemented simple exception handling mechanisms on
top of setjmp/longjmp; Amdahl talked about theirs (used for exception
handling in the kernel) at the last USENIX, and Apollo uses their
implementation in NCS (their remote procedure call package). 

				- Bill



More information about the Comp.lang.c mailing list