C++ has two kinds of exception specifications:
dynamic-exception-specification (deprecated)
which looks like this:
void foo() throw(x, y, z)
In this case, if an exception is thrown within foo
that is not in the set x, y or z, then std::unexpected()
is called. By default, this will call std::terminate()
but you can interpose yourself by setting your own unexpected handler, which may even throw an x, y or z in order to allow the program to continue. Of course, no-one ever does this.
noexcept-specification (since c++11)
which looks like this:
void foo() noexcept; // same as noexcept(true)
void foo() noexcept(true); // not allowed to throw exceptions
void foo() noexcept(false); // allowed to throw exceptions
This is where it gets interesting because the behaviour is different if foo() noexcept(true)
does actually throw an exception. The standard mandates that std::terminate()
shall be called.
You have no opportunity to catch the exception or otherwise correct the situation. A solemn promise was made and broken - end of program.
From §15.4 [except.spec]
10 Whenever an exception of type E is thrown and the search for a handler (15.3) encounters the outermost
block of a function with an exception specification that does not allow E, then,
(10.1) — if the function definition has a dynamic-exception-specification, the function std::unexpected() is
called (15.5.2),
(10.2) — otherwise, the function std::terminate() is called (15.5.1)
const char *
is called by you, not by thephone_number
constructor. The move constructor doesn't throw.