From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Malcolm McLean" Newsgroups: gmane.emacs.help Subject: Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter Date: Sun, 21 Oct 2007 09:03:17 +0100 Message-ID: References: <1192913158.922454.108100@k35g2000prh.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1192965713 23012 80.91.229.12 (21 Oct 2007 11:21:53 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 21 Oct 2007 11:21:53 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Oct 21 13:21:53 2007 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1IjYsQ-00025e-PD for geh-help-gnu-emacs@m.gmane.org; Sun, 21 Oct 2007 13:21:51 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IjYsI-0001dI-UE for geh-help-gnu-emacs@m.gmane.org; Sun, 21 Oct 2007 07:21:42 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!postnews.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.bt.com!news.bt.com.POSTED!not-for-mail Original-NNTP-Posting-Date: Sun, 21 Oct 2007 03:03:16 -0500 Original-Newsgroups: gnu.emacs.help,comp.lang.c,comp.lang.c++ In-Reply-To: <1192913158.922454.108100@k35g2000prh.googlegroups.com> X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Windows Mail 6.0.6000.16480 X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6000.16545 Original-Lines: 130 X-Usenet-Provider: http://www.giganews.com Original-NNTP-Posting-Host: 86.138.9.232 X-AuthenticatedUsername: NoAuthUser Original-X-Trace: sv3-EtcEF5lB1ZfJeZWelntcdgcUNsfYwnoHenVsqSQLiKJ/3rjlP/eQQa87l0OwoMOhGKZitSNpk/2xQmP!bB8P9j9hlVejKUliSTZgp1ECy/FmeSy6kwvDWmSJhBW3NDijjwNsXmAelOdYJXQxK7fcJIPyfA== Original-X-Complaints-To: abuse@btinternet.com X-DMCA-Complaints-To: abuse@btinternet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.36 Original-Xref: shelby.stanford.edu gnu.emacs.help:153158 comp.lang.c:827796 comp.lang.c++:954235 X-Mailman-Approved-At: Sun, 21 Oct 2007 07:20:19 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:48661 Archived-At: wrote in message news:1192913158.922454.108100@k35g2000prh.googlegroups.com... >I have some code like this: > > (if (test) > (exit) > (do something)) > > > or > > (if (test) > ( do something) > (exit)) > > > Various levels of nestings. > > I have several questions, basic to sophisticated. > > (1) What is the lisp equivalent idiom for (exit) as in bash or > in C. > (2) What is the best practice to handle this kind of problems? > > (3) What is the intermediate practice to handle this kind of > problems. > > NOTE: I am really afraid of try-catch-throw. I have never been > able to understand it since it does not exist in C and I cant > really visualize the construct in terms of C. That is what my > brain can process. If you understand it so well, you can show > me how one would really implement that kind of construct in > C and then by extension I can see that kind of program flow > in LISP. Whether its imperative programming or functional, > beneath there is program counter and assembly. C is close > to machine so much that it is almost assembly. So understanding try-c- > t in C is equivalent to understanding at > the level of machine language. > > I therefore take the liberty to crosspost in C and C++ groups. > C++ compilers that compile to C will produce C code something like this C++ double safesqrt(double x) { if(x < 0) throw "imaginary root"; return sqrt(x); } double foo() { if( pow( sqrt(-1), sqrt(-1)) != exp(-M_PI/2) ) printf("Stupid computer can't do basic maths\n"); } int main(void) { try { foo(); } catch(char * err) { printf("Sorry %s\n", err); } } C void * safesqsrt(int *type, double *ret, double x) { if(x < 0) { *type = CHARSTAR; return "imaginary root"; } * ret = sqrt(x); return 0; } void *foo(int *type) { double temp; void *throw; int throwtype; throw = safesqrt(&throwtype, &temp, -1.0); if(throw) { *type - throwtype; return throw; } /* etc */ } int main(void) { char *throw; int throwtype; char *err; throw = foo(&throwtype); if(throw) goto catch; return 0; catch: switch(throwtype) { case CHARSTAR: err = throw; printf("Sorry %s\n", err); break; } } As you see it is totally impractical to try to do this in handwritten C code for very long, though a compiler will happily chug through it. There are in fact more subtleties - local objects in foo() and safesqrt() have to be destroyed. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm