This would make the error pass silently, possibly leaving the program in an inconsistent state. It is probably better to call std::terminate() instead, which is what C++ does when an exception is thrown when another exception is active.
This would make the error pass silently, possibly leaving the program in an inconsistent state. It is probably better to call std::terminate() instead, which is what C++ does when an exception is thrown when another exception is active.
~finally_type() {
try {
function();
} catch (...) {
std::throw_with_nested(std::logic_error
("Exception thrown from \"finally\"."));
}
}
It might pollute calling code a bit with calls to std::rethrow_if_nested(), but I think it’s worth it for the safety.Hence, in most cases where the scope guard pattern is used (to guarantee exception safety), your code is basically equivalent to calling std::terminate().
But anyway, the problem is still programmer error. Just as you shouldn’t throw an exception from a destructor, you shouldn’t throw an exception from a “finally”, because it is, by definition, run in a destructor. So, sure, calling std::terminate() explicitly makes about as much sense as anything, but I’d prefer to ignore (and perhaps log) such erroneous exceptions, for reasons of stability.