Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #1706

Re: Error codes vs. exceptions

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.programming, comp.lang.c++
Subject Re: Error codes vs. exceptions
Date Sun, 03 Jun 2012 11:17:06 +1200
Lines 79
Message-ID <a2vl7iF47cU6@mid.individual.net> (permalink)
References <4f571f71-55ca-4922-b81b-31f954cac855@kw17g2000pbb.googlegroups.com> <jqccel$62o$1@dont-email.me> <3521127b-7a20-4c65-9106-017f519fc860@to5g2000pbc.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net 80MBNoMpioVAznHUFdH3bQHE+sAk6x4DccW35Fsbes+/jtKbGPw+XYRtd8CY4vTkb2
Cancel-Lock sha1:nBrRdGLX8NCSB2+WzBEJlqJ4E5Q=
User-Agent Mozilla/5.0 (X11; SunOS i86pc; rv:12.0) Gecko/20120423 Thunderbird/12.0
In-Reply-To <3521127b-7a20-4c65-9106-017f519fc860@to5g2000pbc.googlegroups.com>
Xref csiph.com comp.programming:1706 comp.lang.c++:16499

Cross-posted to 2 groups.

Show key headers only | View raw


On 06/ 3/12 10:49 AM, mike3 wrote:
> On Jun 2, 12:39 am, "Alf P. Steinbach"<alf.p.steinbach
> +use...@gmail.com>  wrote:
>> On 29.05.2012 06:15, mike3 wrote:
> <snip>
>> All that said, I'm primarily responding because I'm really curious about
>> where this great need for MECHANICAL RULES comes from?
>>
>> If programming could be reduced to mechanical rules, if that was a good
>> idea, then, u know, it could be automated, and then you would not have
>> this problem of finding the rules because you wouldn't be programming:
>> it would be done by machine... So, instead of seeking mechanical rules
>> to be applied mindlessly, I suggest seeking up concrete EXAMPLES of
>> failure handling. Then apply INTELLIGENCE and understanding of concrete
>> situations that you face, and just strive to Keep Things Simple. ;-)
>>
>
> So where could I get examples of _well-written_, _well-designed_,
> _smell-free_ error handling in a _non-trivial_ program?
>
> And also, perhaps I'm not necessarily so interested in rigid rules as
> to
> understand better how to make "good" code.
>
> A big problem is that I seem to find a lot of conflicting information.
> Some say use exceptions, others say use codes, others say use
> exceptions only when "exceptional", otherwise presumably use codes.

I don't think anyone advocates mixing styles.  Pick one or the other.

> But codes have a number of problems of their own (see the thread
> on comp.lang.c where I deal with C where there are no exceptions
> and only codes).

Which is why most modern languages have exceptions.

> Which makes me want to use exceptions. And
> indeed, some say that -- use exceptions. But then I get this thing
> in my head saying "but maybe those people who say it should only
> be used in 'exceptional' circumstances have a good point". And
> then I'm once again confused. And then what "exceptional" means
> is itself, apparently dependent on the speaker.

If you go down the exceptions route, your functions will either have a 
void return or return a value.  Your functions will do what they are 
contracted to do and throw an exception if they are unable to meet their 
obligations.  That may involve the option of spitting some operations 
into two calls (say does item X exist and get item X).  The client code 
can happily call the functions and move on.  The error handling is not 
mixed in with the programme logic, it is collected together in exception 
handler(s).  If that style suits you, follow it.

If you prefer to mix error handling with programme logic, use error 
codes and make sure to check them!

> Would I be right to think the best thing to do here is just pick an
> approach and see where it goes, don't bother worrying who's more
> "right"? Is this just one of those things that simply does not *have*
> a pat, objective answer, and so you have to do some picking and
> choosing and seeing what happens?

Yes, assuming a green field project.  Otherwise use the "when in Rome" 
approach.

> And then I look at examples with other programs, and I see the
> practices someone says "no" to being used! "NO" globals, there's
> a global! And sometimes it's not even just one but a whole lot!
> "NO" GOTOs, and oh my gosh what is that?! A GOTO!!!!! Keep
> methods "under 20 lines", and then I see a lot of huge methods.
> And it doesn't help when the tone I hear from the people and places
> saying "no" is one of near-finality, like these things should only be
> used in RARE circumstances.

Creating rules and ignoring them is a clear sign of a broken process. 
If anyone were to find a goto in my code, I would be obliged to buy them 
a very large quantity of beer.

-- 
Ian Collins

Back to comp.programming | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Error codes vs. exceptions mike3 <mike4ty4@yahoo.com> - 2012-05-28 21:15 -0700
  Re: Error codes vs. exceptions BGB <cr88192@hotmail.com> - 2012-05-29 00:10 -0500
    Re: Error codes vs. exceptions mike3 <mike4ty4@yahoo.com> - 2012-05-28 22:41 -0700
      Re: Error codes vs. exceptions BGB <cr88192@hotmail.com> - 2012-05-29 01:34 -0500
    Re: Error codes vs. exceptions Adam Skutt <askutt@gmail.com> - 2012-05-29 07:16 -0700
      Re: Error codes vs. exceptions BGB <cr88192@hotmail.com> - 2012-05-29 09:45 -0500
  Re: Error codes vs. exceptions Marcel Müller <news.5.maazl@spamgourmet.com> - 2012-05-29 09:08 +0200
    Re: Error codes vs. exceptions mike3 <mike4ty4@yahoo.com> - 2012-05-31 13:52 -0700
      Re: Error codes vs. exceptions Ian Collins <ian-news@hotmail.com> - 2012-06-01 09:08 +1200
        Re: Error codes vs. exceptions mike3 <mike4ty4@yahoo.com> - 2012-05-31 22:32 -0700
  Re: Error codes vs. exceptions "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2012-05-29 09:37 +0200
  Re: Error codes vs. exceptions "io_x" <a@b.c.invalid> - 2012-05-29 10:00 +0200
  Re: Error codes vs. exceptions Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-05-29 12:24 +0000
  Re: Error codes vs. exceptions Nobody <nobody@nowhere.com> - 2012-05-30 15:23 +0100
    Re: Error codes vs. exceptions BGB <cr88192@hotmail.com> - 2012-05-30 09:40 -0500
      Re: Error codes vs. exceptions Adam Skutt <askutt@gmail.com> - 2012-05-30 08:04 -0700
        Re: Error codes vs. exceptions BGB <cr88192@hotmail.com> - 2012-05-30 12:01 -0500
  Re: Error codes vs. exceptions Jeff Flinn <TriumphSprint2000@hotmail.com> - 2012-05-30 14:18 -0400
    Re: Error codes vs. exceptions Patricia Shanahan <pats@acm.org> - 2012-05-30 13:00 -0700
    Re: Error codes vs. exceptions Adam Skutt <askutt@gmail.com> - 2012-05-30 21:25 -0700
  Re: Error codes vs. exceptions Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-05-30 22:44 -0400
    Re: Error codes vs. exceptions Nobody <nobody@nowhere.com> - 2012-05-31 05:04 +0100
    Re: Error codes vs. exceptions Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-06-01 21:44 -0400
  Re: Error codes vs. exceptions "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-06-02 08:39 +0200
    Re: Error codes vs. exceptions BGB <cr88192@hotmail.com> - 2012-06-02 16:31 -0500
    Re: Error codes vs. exceptions mike3 <mike4ty4@yahoo.com> - 2012-06-02 15:49 -0700
      Re: Error codes vs. exceptions Ian Collins <ian-news@hotmail.com> - 2012-06-03 11:17 +1200
      Re: Error codes vs. exceptions Rui Maciel <rui.maciel@gmail.com> - 2012-06-03 16:38 +0100
    Re: Error codes vs. exceptions Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-06-03 20:26 -0400
  Re: Error codes vs. exceptions gremnebulin <peterdjones@yahoo.com> - 2012-06-20 12:05 -0700

csiph-web