On 30/05/2022 13:50, Robert Ramey wrote:
What I actually do in this case is throw a custom exception at the point of failure which gives me information of where I was and what I was doing when I requested this "too large" memory allocation. This seems to be a very reasonable scenario to me.
Technically, the "right" thing to do in this case would be to allow the original bad_alloc to bubble up, perhaps using Boost.Exception, Boost.Leaf, or std::throw_with_nested to decorate it with additional context via catch-and-rethrow. That could potentially be a good use for exceptions (when handled far from the point of actual failure). Although it can still be argued that trying to make a too-large memory allocation is usually the result of a failure of input sanitisation (e.g. not checking data from input files), which is still a kind of coding bug.