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


Groups > comp.programming > #1670

Re: Error codes vs. exceptions

From BGB <cr88192@hotmail.com>
Newsgroups comp.programming, comp.lang.c++
Subject Re: Error codes vs. exceptions
Date 2012-05-30 12:01 -0500
Organization albasani.net
Message-ID <jq5jso$1on$1@news.albasani.net> (permalink)
References <4f571f71-55ca-4922-b81b-31f954cac855@kw17g2000pbb.googlegroups.com> <pan.2012.05.30.14.23.06.552000@nowhere.com> <jq5bks$ere$1@news.albasani.net> <9e8f2d10-2159-4c54-bb76-ead7db24dc00@r3g2000yqh.googlegroups.com>

Cross-posted to 2 groups.

Show all headers | View raw


On 5/30/2012 10:04 AM, Adam Skutt wrote:
> On May 30, 10:40 am, BGB<cr88...@hotmail.com>  wrote:
>> On 5/30/2012 9:23 AM, Nobody wrote:
>>
>>> On Mon, 28 May 2012 21:15:00 -0700, mike3 wrote:
>>
>>>> I've heard about this, and wonder when is it right to use codes, and
>>>> when to use exceptions for reporting errors? I've heard various stuff,
>>>> such as that exceptions should only be used to indicate "exceptional"
>>>> conditions. Yet what does that mean?
>>
>>> It usually means that the condition will rarely occur in practice.
>>
>>> Performance-wise, exceptions are costly if the exception is actually
>>> thrown but free if not thrown, while a status return has a constant
>>> overhead. If the condition rarely occurs, exceptions will work out
>>> cheaper; if the condition is common, returning a status will be cheaper.
>>
>> pretty much.
>>
>> a status code also has the advantage if the return value is used to
>> drive logic in the caller, such as knowing when a task has finished,
>> when a desired item has been found, ...
>>
>
> Or you just return the item and avoid the need for status altogether.
> Success should be normally be implicit.
>

only if one knows that the function will almost always succeed (IOW: 
succeeding is part of its "definition of operation").

if it may also fail as part of its normal/expected operation, then it no 
longer is so clear cut.


succeeding/failing need not be the same as an error/non-error condition, 
but may be due to other factors (such as an item not already being in a 
list or hash-table and needing to be created, ...).

not all status codes indicate errors as well, and there may actually be 
successful status codes as well (traditionally these are distinguished 
by sign, where <0 means failure, and >=0 means success).


>>> But performance is seldom the main issue. If throwing an exception means
>>> that typical code will just wrap the call in a try/catch block, you should
>>> use a status return instead. If typical code will propagate errors up the
>>> call stack, you should probably use an exception.
>>
>> agreed.
>>
>> exceptions work much better for transferring control across multiple
>> levels of call frames.
>>
>> if the handler logic is directly in the caller, then it is less convenient.
>
> You've said this a whole bunch of times but never once demonstrated
> how this is actually true.  I can think of a few cases where it's
> true, but they're all pretty contrived.
>

I make heavy use of code driven by "predicate functions" (functions or 
methods which return a boolean value to indicate status), which are 
arguably the same sort of thing as what you are arguing against.

it is all a fairly pointless and nit-picky argument at this point.



>>
>> similarly, there may be the matter of seriousness:
>> if the situation is "serious" (as-in, could potentially compromise
>> correct functioning of the application), then an exception may make more
>> sense;
>> if the situation is something which could be reasonably ignored with
>> little or no consequence, then a status code may make more sense.
>>
>
> As I've explained to you before, it's normally impossible for the
> function issuing the error to tell the difference between these two
> situations.  Again, you've never once demonstrated how to apply this
> reasoning to writing correct, usable code.
>

usually a person writing an application will know what the code in the 
thing is going to be doing, as well as the consequences of a given 
operation succeeding or failing.

usually it is also "fairly obvious" from what the function is going to 
be doing as well.


now, I am not opposing using exceptions here, by any means, as there are 
many cases where they do make sense (namely, cases where the situation 
*actually is* unexpected or abnormal).

for example:
running out of memory, or detecting a problem with the heap, is 
something a bit more worthy of an exception.

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