Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 Exception's destructor allows to throw now?

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;
}
like image 797
hong pei Avatar asked May 14 '13 10:05

hong pei


1 Answers

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-id T if and only if T 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, and f has the exception-specification noexcept(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.

like image 68
Andy Prowl Avatar answered Sep 29 '22 02:09

Andy Prowl