Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!news.funet.fi!tukki.jyu.fi!jyu.fi!otto From: otto@tukki.jyu.fi (Otto J. Makela) Newsgroups: comp.lang.c Subject: Re: Error Handling Summary: Use gotos, and sin no more Message-ID: Date: 28 Sep 90 07:09:52 GMT References: <837@babcock.cerc.wvu.wvnet.edu> Sender: news@tukki.jyu.fi (News articles) Organization: Turing Police, Criminal AI section Lines: 40 In-Reply-To: vrm@cathedral.cerc.wvu.wvnet.edu's message of 27 Sep 90 15:15:21 GMT In article <837@babcock.cerc.wvu.wvnet.edu> vrm@cathedral.cerc.wvu.wvnet.edu (Vasile R. Montan) writes: [What is the best way for a function to test for errors ?] Method 1: Return Error Code When Error Is Encountered The problems I have with this are first it goes against software engineering principles which say that all functions should have one entry point and one exit point. Secondly some functions do have some standard clean up to do. [...] Method 2: Set Error Code and Return Only at End The problem I have with this is that I don't want to do later work if a previous error occured. I just want to clean up and return. This forces me to continuously check if a previous error occured. [...] Method 3: Use GOTO to Create Exception Handler Of course this breaks the golden rule of software engineering of "absolutely positively no GOTO's in your program." I would go for method 3 everytime (if the clean-up is trivial, go for 1). Remember, rules like "no GOTOs" are inventions of Pascal weenies, and I wouldn't call them golden :-) Simplistic rules like this are supposedly a alternative to using common sense in programming (no smiley here). They are human inventions, they didn't come down from the mountain engarved in stone tablets. Seriously, do you think method 2 is more "structured" than method 3 only because it has no gotos in it ? I have seen awful constructs which are supposedly structured programming, in which you have a dozen of "break- out-flags" from nested loops because you can't use gotos or breaks (the latter being only gotos to the exit point of the loop). (I have a weird feeling I'm going to be flamed by religiously structured zealots... what are you doing with that napalm ?) -- /* * * Otto J. Makela * * * * * * * * * * * * * * * * * * */ /* Phone: +358 41 613 847, BBS: +358 41 211 562 (CCITT, Bell 24/12/300) */ /* Mail: Kauppakatu 1 B 18, SF-40100 Jyvaskyla, Finland, EUROPE */ /* * * Computers Rule 01001111 01001011 * * * * * * * * * * * * * * * * */