Is this keyword a bit of counter-intuitive ?
The function won't throw any exceptions so we add a throw() tag to it. Should it be silent() or something like that?
Is this keyword a bit of counter-intuitive ?
The function won't throw any exceptions so we add a throw() tag to it. Should it be silent() or something like that?
From a pre-C++17 perspective: it may be counter-intuitive, but only for a moment. throw(type, type, ...)
provides a list of types of exceptions that the function may throw. throw()
is supplying an empty list of types, i.e. the function cannot throw any type of exception.
throw()
= can throw exactly those exceptions:""
That is, can't throw anything.
Historically, a real list was provided: throw(X,Y,Z)
meant "can throw X
, or Y
or Z
". That syntax was removed because it wasn't really useful: the most useful guarantee is that a function can't throw anything.
A function can optionally be declared with a throw()
specifier, known as an exception specification, to list the type of exceptions it is allowed to throw. If the list is empty, the function is not allowed to throw any exception at all. If the function has a throw()
specifier and throws an exception that is not in the list, the program is terminated immediately by default (this behavior can be customized at runtime).
This uses of a throw()
specifier was deprecated in C++11, and the specific case of an empty throw()
specifier was replaced with the new noexcept
specifier.
The non-empty throw()
specifier was removed from the language in C++17, and the empty throw()
specifier was removed in C++20.
The keyword is deprecated, in current standard it's 'noexcept'