Is nothrow / noexcept enough to say we don't have a throw guarantee?

In Exception Safety, created by Abraham, there are three guarantees: basic, strong, and no throw. May I say that if I have a codebase using nothrow for "new" and noexcept for method signatures, I have a no-throw guarantee?

Thank.

+3


source to share


2 answers


No, this is not the correct conclusion. The no-throw guarantee does not mean that you are not throwing exceptions, it means that the operation always succeeds. A code base using an allocator that returns nullptr

on failure and indicates function failures by returning a status code doesn't quite match this up. There is also no codebase that triggers abort()

on any detected error. However, both of them are perfectly possible with the functions new(nothrow)

and noexcept

.



+2


source


You will have to avoid (or handle and recover from possible failures) the following language constructs:

  • dynamic_cast

    to a reference type that will throw std::bad_cast

    if the conversion fails;
  • typeid

    applies to the result of dereferencing a potentially null pointer that will be thrown std::bad_typeid



And of course you will have to avoid most of the standard library. In particular, things like containers use allocators that can signal failure by throwing.

You will struggle to make sure there is no throwing around the "codebase" (if that means you mean the code for an entire program or library), since the operations that you might expect will usually be performed; they can provide the maximum guarantee.

+2


source







All Articles