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


Groups > comp.lang.c++ > #41204

Re: asserts considered harmful

From "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject Re: asserts considered harmful
Date 2016-02-24 01:02 +0100
Organization A noiseless patient Spider
Message-ID <nairpf$p3c$1@dont-email.me> (permalink)
References <8_2dnQWwaKOXRlHLnZ2dnUU7-dudnZ2d@giganews.com> <naio4b$9ic$1@dont-email.me> <6a6dndcqc5TVdFHLnZ2dnUU7-eednZ2d@giganews.com>

Show all headers | View raw


On 24.02.2016 00:20, Mr Flibble wrote:
> On 23/02/2016 23:00, Alf P. Steinbach wrote:
>> On 23.02.2016 23:19, Mr Flibble wrote:
>>
>>> One should prefer exceptions to asserts.  One should have many exception
>>> types and many throws (a throw statement should have no performance
>>> impact in a decent implementation for the code path where no exception
>>> is thrown) but few try/catches.
>>
>> I think this is much like an argument that one should prefer runtime
>> type checking, namely based on a conflation of issues, and designed to
>> make one particular chosen approach to things seem preferable.
>>
>> An assertion expresses an INCORRECT ASSUMPTION, strongly associated with
>> preconditions.
>>
>> This is a contract breach by a caller, or by preceding code, and it
>> indicates something wrong up there, often with no way to be sure exactly
>> where or what.
>>
>> Throwing an ordinary exception misinforms the calling code, which will
>> handle the ordinary exception as if this code, not itself, had failed.
>>
>> An exception (a standard exception) throwing, OTOH., expresses a GOAL
>> ACHIEVEMENT FAILURE, strongly associated with postconditions.
>>
>> This is a local failure. One knows what's wrong, e.g. some resource
>> exhausted, some item not found, whatever, including the case where some
>> lower level function failed, possibly via exception. And the design
>> called for such failure to be communicated back up via an exception.
>
> Not true: std::logic_error is meant for coding errors including
> incorrect assumptions.  One should derive custom exceptions from
> std::logic_error and throw these instead of asserting.

Well, it's certainly true that SOMEONE once meant you to use 
`std::logic_error` in that way, otherwise it wouldn't be there.

Counting against that, when thrown from deep down in a call chain 
`std::logic_error` is almost guaranteed to be caught by a general 
`std::exception` handler, and this code will treat it as a failure by 
the throwing code – instead of as indicating its own failure. So it's 
almost sure to be handled in the Wrong Way™. I consider this, about very 
concrete advantages/disadvantages, to be much more important for my 
use/don't use decision than the SOMEONE's opinion somewhere pre 1998.

Are there other indications, beside `std::logic_error`, that the 
standard exception hierarchy & machinery is not at all well designed?

Yes, for example that these critical exceptions can do dynamic allocation.

And for example, that the semantics of `throw` exception specifications 
were never implemented by Microsoft and were deprecated in C++11.

Oh, and `std::uncaught_exception`.

Not to mention the extreme shenanigans one must go to, complex and 
inefficient, with internal re-throwing and catching, to extract full 
exception information with C++11 and later's nested exceptions.

Well, and more.

So, regarding what to use, I don't assign much weight to the SOMEONE's 
authority. ;-)


- Alf

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-23 22:19 +0000
  Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 11:27 +1300
    Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-23 22:32 +0000
      Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 11:44 +1300
    Re: asserts considered harmful Cholo Lennon <chololennon@hotmail.com> - 2016-02-24 16:49 -0300
  Re: asserts considered harmful "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-24 00:00 +0100
    Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-23 23:20 +0000
      Re: asserts considered harmful "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-24 01:02 +0100
        Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-24 00:17 +0000
          Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 13:25 +1300
      Re: asserts considered harmful Vir Campestris <vir.campestris@invalid.invalid> - 2016-02-24 21:27 +0000
        Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-25 10:59 +1300
        Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 14:13 -0800
          Re: asserts considered harmful scott@slp53.sl.home (Scott Lurndal) - 2016-02-25 00:12 +0000
            Re: asserts considered harmful David Brown <david.brown@hesbynett.no> - 2016-02-25 01:23 +0100
              Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-25 15:29 +1300
                Re: asserts considered harmful Vir Campestris <vir.campestris@invalid.invalid> - 2016-02-25 23:05 +0000
                Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-26 19:41 +1300
                Re: asserts considered harmful David Brown <david.brown@hesbynett.no> - 2016-02-26 17:11 +0100
            Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 17:32 -0800
    Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 13:00 +1300
      Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-24 00:24 +0000
        Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 13:34 +1300
          Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-24 01:21 +0000
            Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 14:55 +1300
            Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-23 18:47 -0800
              Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-24 17:52 +0000
                Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 11:14 -0800
                Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-24 19:42 +0000
                Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 12:58 -0800
                Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-24 21:49 +0000
                Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 14:03 -0800
          Re: asserts considered harmful Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-24 11:48 +0200
            Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 23:14 +1300
            Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 05:52 -0800
          Re: asserts considered harmful woodbrian77@gmail.com - 2016-03-09 11:32 -0800
      Re: asserts considered harmful mark <mark@invalid.invalid> - 2016-02-24 14:43 +0100
        Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 06:22 -0800

csiph-web