Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecated throw-list in C++11

Tags:

c++

c++11

throw

Just as I can see in cppreference, the classic "throw" declaration lists is now deprecated in C++11. What is the reason of leaving this mechanism and how should I have to specify what exceptions throws a function of mine?

like image 509
Peregring-lk Avatar asked Dec 12 '12 14:12

Peregring-lk


People also ask

Why are dynamic exception specifications are deprecated in C++ 11?

Dynamic exception specifications are deprecated. That's a warning from the standards committee that they might be removed in a future version of the standard. In the meantime, they are legal and their behavior is well defined.

How do I fix ISO C++ 17 does not allow dynamic exception specifications?

The way to fix it is to remove the throw specification completely, unless it's throw() , which is replaced by noexcept . The End. setting cpp standard to 11 or 14 should have fixed it.

What happens when Noexcept function throws?

noexcept means that a function will not throw, not that it cannot throw, and the penalty for failure to comply is calling std::terminate , not UB.

Which function declaration states that no exceptions will be thrown by a method?

A function with an exception specification that has an empty type_id_list, throw() , does not allow any exceptions to be thrown. An exception specification is not part of a function's type.


1 Answers

For more detailed reasoning, see: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3051.html

As expressed in the national body comment above, exception specifications have not proven useful in practice. There are numerous discussions of the problems with exception specifications in C++ (see, e.g., [Sutter02], [Boost03]), but the main issues are:

  • Run-time checking: C++ exception specifications are checked at runtime rather than at compile time, so they offer no programmer guarantees that all exceptions have been handled. The run-time failure mode (calling std::unexpected()) does not lend itself to recovery.
  • Run-time overhead: Run-time checking requires the compiler to produce additional code that also hampers optimizations.
  • Unusable in generic code: Within generic code, it is not generally possible to know what types of exceptions may be thrown from operations on template arguments, so a precise exception specification cannot be written.

In practice, only two forms of exception-throwing guarantees are useful: an operation might throw an exception (any exception) or an operation will never throw any exception. The former is expressed by omitting the exception-specification entirely, while the latter can be expressed as throw() but rarely is, due to performance considerations.

[N3050] introduces a new kind of exception specification, noexcept, the specifies that the function will not throw any exceptions. Unlike throw(), noexcept does not require the compiler to introduce code to check whether an exception is thrown. Rather, if a function specified as noexcept is exited via an exception, the result is a call to std::terminate().

With the introduction of noexcept, programmers can now express the two kinds of exception guarantees that are useful in practice, without additional overhead. This paper therefore proposes to deprecate "dynamic" exception specifications, i.e., those that are written as throw(type-id-listopt).

like image 185
Peter Avatar answered Oct 08 '22 13:10

Peter