0

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?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Koren.H
  • 9
  • 3

4 Answers4

4

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.

hobbs
  • 223,387
  • 19
  • 210
  • 288
1

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.

curiousguy
  • 8,038
  • 2
  • 40
  • 58
1

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
-3

The keyword is deprecated, in current standard it's 'noexcept'

zmij
  • 57
  • 7
  • No, `throw` is used to throw exceptions. – Bathsheba Nov 01 '19 at 08:20
  • Clarification: the **keyword** is used in multiple places, including _throw expressions_ and _exception specifications_. See Remy Labeau's answer on the latter. The keyword isn't deprecated and in fact is in active use for throw statements. – MSalters Nov 01 '19 at 09:45
  • The keyword is deprecated for exception specification – zmij Nov 11 '19 at 11:34