From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Jim Langston" Newsgroups: gmane.emacs.help Subject: Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter Date: Sat, 20 Oct 2007 18:08:11 -0700 Message-ID: <2ExSi.169$GB1.96@newsfe06.lga> References: <1192913158.922454.108100@k35g2000prh.googlegroups.com> NNTP-Posting-Host: lo.gmane.org X-Trace: ger.gmane.org 1192953104 24459 80.91.229.12 (21 Oct 2007 07:51:44 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 21 Oct 2007 07:51:44 +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 09:51:45 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 1IjVb5-0008F7-R0 for geh-help-gnu-emacs@m.gmane.org; Sun, 21 Oct 2007 09:51:44 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IjVay-0007cg-7v for geh-help-gnu-emacs@m.gmane.org; Sun, 21 Oct 2007 03:51:36 -0400 Original-Path: shelby.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!hwmnpeer01.lga!news.highwinds-media.com!hw-filter.lga!newsfe06.lga.POSTED!53ab2750!not-for-mail Original-Newsgroups: gnu.emacs.help,comp.lang.c,comp.lang.c++ Original-Lines: 123 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 X-RFC2646: Format=Flowed; Original Original-NNTP-Posting-Host: dgbhgachkpdlnliepgjkkbmmojpjailc Original-X-Trace: emlnjhpjaknjmmohlbdnpiohcfhaihogjpeefcfohmmolccpdgbhgachkpdlnliemjomgnkeiejlbjcigijdaejdoaakhkhgoiddoondpffjcbiianicllgoaaiabhikdajcnicogimfpado Original-NNTP-Posting-Date: Sat, 20 Oct 2007 18:08:14 MST Original-Xref: shelby.stanford.edu gnu.emacs.help:153150 comp.lang.c:827779 comp.lang.c++:954221 X-Mailman-Approved-At: Sun, 21 Oct 2007 03:50:32 -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:48658 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. Your message post is asking "How to exit out of a function..." yet you are showing code that almost exits out of the program. exit() would do that. If you wish to exit out of the function then the correct keyword is return. Such as: if (condition) return; (do something) if ( condition ) (do something ) return; return can return a value if the function returns a value, such as return 1; return foo; return bar(); etc.. > (2) What is the best practice to handle this kind of problems? It depends on coding style. There is some coding style that states that any function will only have one return point. In which case you would do: if ( condition ) ( do something ) return; Personally, I chose to return early if it's what I would consider an error condition. if ( condition ) return NULL; ( do something ) return Value; There really is no best way, it depends on coding standard and how easy the code is to write, read and maintain. > (3) What is the intermediate practice to handle this kind of > problems. Again, it depends on the problems. At different times in my career I have gone with returning whenever I've wanted to to returning only at the very end. I find that huge if statments, however, make my code harder to write, read and maintain, so I will check for conditions early and return early if I can. I try not to return in the middle of a function, but only at the top or bottom. But it depends on the situation. > 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. As for try...catch blocks, I try to reserve those only for actual errors. One reason I have found I need to use them when a function is supposed to return a reference to an object, and that object doesn't exist. I have to return something, and something is not good enough. So I get around it by throwing out of the function. (Untested code) Player& FindPlayer( const std::string& Name, std::map ) { std::map::iterator it = Player.find( Name ); if ( it == Player.end() ) { // Player was not found in map. Have to return something. Lets just throw. throw std::exception("Player not found"); } return (*it).second; } int main() { // ... try { Player ThisPlayer& = FindPlayer( SomePlayer ); // use ThisPlayer } castch ( std::exception e ) { std::cout << "Player " << SomePlayer << " not found.\n"; } }