How to detect stack unwinding in a destructor
c++, exception, stack-unwinding
Solution
Most people have used `std::uncaught_exception()` to try to tell if an exception is pending, so they can throw an exception from a destructor if there isn't one already. That is generally considered Not A Good Idea.
If you want to not undo an operation if an exception has thrown, it should do the trick.
Remember that the destructor is your last chance to release any resources an object has, because after the destructor ends the object does not exist, and any resources it held are now permanently leaked. If `OpDo()` allocates any memory or file handles or whatever, you do need to deal with that in the destructor no matter what.
Problem
I have a simple C++ object that I create at the start of function `F()` to ensure two matched functions (OpDo, OpUndo) are called at the start and return of the `F()`, by using the object's constructor and destructor. However, I don't want the operation to be undone in case an exception was thrown within the body of `F()`. Is this possible to do cleanly? I have read about `std::uncaught_exception`, but its use does not seem to be recommended.