I'm trying to understand why the destructors throwing exceptions is a very bad idea. By googling, I understand that if destructor throws, say, during destruction of the block-scope objects, the destruction of the obeject stopped and we get memory leak if there're the other objects destructors have not called for.
But by itself, the destructor throwing exceptions is so bad? For instance (the complete example):
struct A
{
int a;
char b;
A() : a(10), b('a') { }
~A(){ throw std::exception(); }
};
int main()
{
A a; //destructor throws, but there're no more objects to destruction
//therefore I think there's no memory leak
}
Do we get UB in the program above?
A
does have a problem, if it ends up being used in any non-trivial manner. I'm not quite sure about the Undefined Behavior question though, I'm not a language-lawyer for C++. - DanielA
and using it in a more complicated program. There are lots of existing Q&A about the issues, e.g. here. - Tony Delroy