> Because C++ kinda-should’ve-but-really-doesn’t have checked exceptions, we must simply discard them:
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.
You’re quite right that ignoring the (programmer) error is too lax, but std::terminate() is far too extreme. This is perhaps a situation for std::nested_exception:
No, if the finally clause is called while an exception is active, the runtime will call std::terminate(). This is why it is bad practice to throw exceptions in destructors.
Hence, in most cases where the scope guard pattern is used (to guarantee exception safety), your code is basically equivalent to calling std::terminate().
Alright, yeah, no exception should escape the finally destructor. After so many years of using it, I should know better than to write C++.
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.
Which kinda goes to my point about out of memory exceptions - they generally end up being fatal sooner or later. Trying to catch them just results in memory running out somewhere else. The only place you can deal with them is if the code is doing something you expect to cause memory issues because it is consuming an atypically large amount of memory (like a cache or some such) which you can get away with shrinking.
Comments
> Because C++ kinda-should’ve-but-really-doesn’t have checked exceptions, we must simply discard them:
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.
You’re quite right that ignoring the (programmer) error is too lax, but std::terminate() is far too extreme. This is perhaps a situation for std::nested_exception:
It might pollute calling code a bit with calls to std::rethrow_if_nested(), but I think it’s worth it for the safety.No, if the finally clause is called while an exception is active, the runtime will call std::terminate(). This is why it is bad practice to throw exceptions in destructors.
Hence, in most cases where the scope guard pattern is used (to guarantee exception safety), your code is basically equivalent to calling std::terminate().
Alright, yeah, no exception should escape the finally destructor. After so many years of using it, I should know better than to write C++.
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.
Which kinda goes to my point about out of memory exceptions - they generally end up being fatal sooner or later. Trying to catch them just results in memory running out somewhere else. The only place you can deal with them is if the code is doing something you expect to cause memory issues because it is consuming an atypically large amount of memory (like a cache or some such) which you can get away with shrinking.