C++11 noexcept Keyword

C++11 includes another concept based keyword noexcept. This keyword can be used for specifying that any function cannot throw — or is not ready to throw.

Here is a code snippet:

void test() noexcept;

This declares that test() won't be able to throw. If the exception is not dealt with locally inside test() — and when test() method throws — the program will get terminated, calling std:: terminate() method that by default calls the std:: abort().

noexcept focuses on a lot of issues (empty) exception a specification is having:

  • Runtime checking: Exceptions in terms of C++ get checked at runtime rather than at compile time, and hence they offer no programmer guarantees whether all exceptions got to be handled or not. The runtime failure mode (which internally calls std:: unexpected()) does not lend itself to recovery.
  • Runtime overhead: Checking at runtime needs the compiler producing additional code which may also hamper optimizations.
  • Unfeasible in generic code: Contained by any generic code, it is not usually possible to know what types of exceptions will get thrown on template arguments and hence an exact exception specification can't be written.