Boost logo

Boost :

Subject: Re: [boost] Noexcept
From: Jens Finkhäuser (jens_at_[hidden])
Date: 2017-06-20 09:40:23


On Tue, Jun 20, 2017 at 01:32:41AM -0700, Emil Dotchevski via Boost wrote:
> Yes, std::logic_error is another embarrassment for C++. Logic errors by
> definition leave the program in an undefined state, the last thing you want
> to do in this case is to start unwinding the stack. You should use an
> assert instead.
  That's true enough for applications, less so for libraries. A
library may simply be re-initialized upon throwing a logic error (if it permits),
and the using application may try to use the library's functionality again with
modified input data. That won't, of course, fix the logic error, but
it may circumvent it and produce useful application output.
  Python effectively achieves this by having assert raise a catchable
AssertionError.

> Exceptions are not used in case of "irregularities" but to enforce
> postconditions. When the program throws, it is in well defined state,
> working correctly, as if the compiler automatically writes "if" statements
> to check for errors before it executes any code for which it would be a
> logic error if control reaches it. The _only_ cost of this goodness is that
> your code must be exception safe.
>
> Programmers who write debuggers that by default break when a C++ exception
> is thrown likely do not understand the semantic differences between OS
> exceptions (e.g. segfaults, which *do* indicate logic errors) and C++
> exceptions. Semantically, that's like breaking, by default, every time a C
> function returns an error code.
  Well, no. C functions return error codes also as both a way to
describe exceptional conditions (e.g. ENOMEM), user error (e.g. EINVAL) as
well as informing the user of what they should do (e.g. EAGAIN). (I
prefer to treat them as status codes for this reason.)
  Exceptions can only cover the first two cases, and only the first is
non-recoverable. Breaking in those cases makes more sense than
breaking when C functions return error codes precisely because of the
likes of EAGAIN.

  Effectively, C++ exceptions occupy the middle ground between
unrecoverable error states and status codes, i.e. situations that
users may wish to recover from, but that should lead to program
terminiation if they choose not to.

  One *good* example is throwing a logic_error in the default case of a
switch statement (specifically a domain_error, in this case). Not in every
situation, of course.

I hope that makes sense,
  Jens

-- 
1.21 Jiggabytes of memory ought to be enough for anybody.



Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk