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


Groups > comp.programming > #1714

Re: Error codes vs. exceptions

From Rui Maciel <rui.maciel@gmail.com>
Newsgroups comp.programming, comp.lang.c++
Subject Re: Error codes vs. exceptions
Followup-To comp.lang.c++
Date 2012-06-03 16:38 +0100
Organization Aioe.org NNTP Server
Message-ID <jqg0dv$eb5$1@speranza.aioe.org> (permalink)
References <4f571f71-55ca-4922-b81b-31f954cac855@kw17g2000pbb.googlegroups.com> <jqccel$62o$1@dont-email.me> <3521127b-7a20-4c65-9106-017f519fc860@to5g2000pbc.googlegroups.com>

Cross-posted to 2 groups.

Followups directed to: comp.lang.c++

Show all headers | View raw


mike3 wrote:

> 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.
> 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 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.

In some cases, both exceptions and error codes are adequate solutions.  
Therefore, it isn't necessarily wrong or a bad idea to use either one in 
those cases, and the decision to pick one tends to be a design decision.  
Hence the apparently conflicting suggestions.

Personally, I see exceptions as a way to handle errors in a sequence point 
other than the one immediately after where the errors are thrown.  As a 
downside, when you throw an exception you explicitly declare that you don't 
know or even care which sequence point will be executed next.  You trust 
that the exception catching code does the right thing, and that the 
program's execution will resume in a safe, innocuous sequence point.


> 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?

That sounds like a recipe for a mild disaster.  After all, if you use a tool 
without even thinking if it is the right one for your job then how do you 
even know it is a good idea to use it?

A decent rule of thumb that helps see which solution is better suited is as 
follows:
- if you are dealing with exceptional circumstances which can be omitted 
from the control flow of your code without jeopardizing clarity or 
readability (i.e., memory allocation errors, sanity checks failing in an 
unexpected way, network connections breaking up, etc...) then exceptions may 
be a good way to go.
- if, instead, you are dealing with expectable errors which directly 
influence the control flow of your code to the point you can't express an 
algorithm without explicitly covering them then returning error codes may be 
a good way to go.

Other than this, it's a matter of understanding how exceptions and error 
codes work, particularly their downsides, and make a design decision on 
which one to pick to handle a particular error.


Rui Maciel

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