any idea why virtual ~exception() throw() is in C++98, but virtual ~exception() is in C++11?
What's the design decision that allows C++11 to throw in the destructor of the class exception
?
From here:
c++98:
class exception {
public:
exception () throw();
exception (const exception&) throw();
exception& operator= (const exception&) throw();
virtual ~exception() throw();
virtual const char* what() const throw();
}
c++11:
class exception {
public:
exception () noexcept;
exception (const exception&) noexcept;
exception& operator= (const exception&) noexcept;
virtual ~exception();
virtual const char* what() const noexcept;
}
What's the desing decision makes C++11 allow to throw in the destructor of the class
exception
?
There was no such design decision (fortunately!). In C++11, even explicitly declared destructors are qualified as noexcept
by default. This can be evinced from paragraph 12.4/3 of the C++11 Standard:
A declaration of a destructor that does not have an exception-specification is implicitly considered to have the same exception-specification as an implicit declaration (15.4).
And from paragraph 15.4/14, which specifies what exception specification an implicit declaration has:
An inheriting constructor (12.9) and an implicitly declared special member function (Clause 12) have an exception-specification. If
f
is an inheriting constructor or an implicitly declared default constructor, copy constructor, move constructor, destructor, copy assignment operator, or move assignment operator, its implicit exception-specification specifies the type-idT
if and only ifT
is allowed by the exception-specification of a function directly invoked by f’s implicit definition;f
allows all exceptions if any function it directly invokes allows all exceptions, andf
has the exception-specificationnoexcept(true)
if every function it directly invokes allows no exceptions.
Together, the above two paragraphs guarantee (given the declaration you quoted of exception
's destructor) that the destructor of exception
won't throw.
This is also explicitly stated in paragraphs 18.8.1/7-8 of the C++11 Standard:
virtual ~exception();
7 Effects: Destroys an object of class exception.
8 Remarks: Does not throw any exceptions.
Notice, that dynamic exception specifications (such as throw()
) are deprecated in C++11. Per § D.4/1 of the Annex D:
The use of dynamic-exception-specifications is deprecated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With