Reputation: 1830
In our application, we log any crashes into a log file with stack trace included. We can use these reports to identify crash causes.
The problem is, that we tend to catch std::exception on several places (a lot actually), which makes the report effectively useless when bad_alloc is thrown, as the stack trace is lost.
How to change the behaviour, so instead of throwing bad_alloc, the program aborts? As we write in 3 different operating systems, so 3 different std implementations are used, changing the std itself is something we would like to avoid.
Upvotes: 2
Views: 114
Reputation: 1830
I checked the exception hierarchy (http://en.cppreference.com/w/cpp/error/exception) and it seems, that we never need to catch anything outside std::runtime_exception and all of our internal exception types are derived from std::runtime_exception.
So I just changed that the broadest catch we have in our program is std::runtime_error so std::bad_alloc is becomes exception, which we can properly manage.
Edit: This can only be used since C++11
Upvotes: 0
Reputation: 409356
Besides a rethink or redesign to catch more tailored exceptions (which I really recommend) you have two solutions:
Use the "no-throw" variants of operator new
and operator new[]
. Check for a returned null pointer, and abort.
Set a new
handler that calls std::terminate
.
Upvotes: 7