std::set_new_handler
Min standard notice:
Header: <new>
Makes new_p the new global new-handler function and returns the previously installed new-handler.
# Declarations
std::new_handler set_new_handler( std::new_handler new_p ) throw();
(until C++11)
std::new_handler set_new_handler( std::new_handler new_p ) noexcept;
(since C++11)
# Parameters
new_p: pointer to function of type std::new_handler, or null pointer
# Return value
The previously-installed new handler, or a null pointer value if none was installed.
# Example
#include <iostream>
#include <new>
void handler()
{
std::cout << "Memory allocation failed, terminating\n";
std::set_new_handler(nullptr);
}
int main()
{
std::set_new_handler(handler);
try
{
while (true)
{
new int[1000'000'000ul]();
}
}
catch (const std::bad_alloc& e)
{
std::cout << e.what() << '\n';
}
}