Petr Petr - 26 days ago 19
C++ Question

why C++11 mark destructors as nothrow, and is it possible to override it?

So far I never had a single warning emitted by a C++ compilers but now VS 2015 compiler seems to suddenly start complaining about this.

It seems that C++11 implicitly mark every destructor as nothrow https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(C4297)&rd=true

Why is that? It may be a bad idea, but I would like to understand why? And if it's not inherently bad, is there a way to override it so that destructor is not a nothrow?

P.S. I know that exceptions may be evil, but sometimes they are usefull, especially when generating crash reports from users of my app.

Answer

Destructors are called when scopes are exited. When an exception is thrown, the stack is unwound which causes scopes to be exited and destructors to be called. When an exception is thrown while another is in progress, the process is unconditionally aborted with std::terminate. That is why throwing from destructors is a bad idea.

If you want to do it anyway, you can explicitly mark a destructor noexcept(false).

Comments