Is there any difference between throw() and noexcept other than being checked at runtime and compile time respectively?
This Wikipedia C++11 article suggests that the C++03 throw specifiers are deprecated.
Why so … Is the noexcept capable enough to cover all that at compile time?
Note: I checked this question and this article, but couldn’t determine the solid reason for its deprecation.
Exception specifiers were deprecated because exception specifiers are generally a terrible idea.
noexceptwas added because it’s the one reasonably useful use of an exception specifier: knowing when a function won’t throw an exception. Thus it becomes a binary choice: functions that will throw and functions that won’t throw.noexceptwas added rather than just removing all throw specifiers other thanthrow()becausenoexceptis more powerful.noexceptcan have a parameter which compile-time resolves into a boolean. If the boolean is true, then thenoexceptsticks. If the boolean is false, then thenoexceptdoesn’t stick and the function may throw.Thus, you can do something like this:
Does
CreateOtherClassthrow exceptions? It might, ifT‘s default constructor can. How do we tell? Like this:Thus,
CreateOtherClass()will throw iff the given type’s default constructor throws. This fixes one of the major problems with exception specifiers: their inability to propagate up the call stack.You can’t do this with
throw().