D
desktop
I have made the following new_handler that I set in main:
/* ================== new_handler ================== */
void no_mem() {
std::cerr << "out of memory\n";
throw std::bad_alloc();
std::exit(1);
}
void init(int length, int* data) {
try {
data = new int [length];
}
catch(std::bad_alloc){
std::cerr<< "out of memory\n";
}
}
int main() {
// Initialize the new_handler.
std::set_new_handler(&no_mem);
int* data;
int i = 4;
init(i,data);
return 0;
}
But is it necessary to catch the exception in "init" or will it also be
caught by the new_handler?
/* ================== new_handler ================== */
void no_mem() {
std::cerr << "out of memory\n";
throw std::bad_alloc();
std::exit(1);
}
void init(int length, int* data) {
try {
data = new int [length];
}
catch(std::bad_alloc){
std::cerr<< "out of memory\n";
}
}
int main() {
// Initialize the new_handler.
std::set_new_handler(&no_mem);
int* data;
int i = 4;
init(i,data);
return 0;
}
But is it necessary to catch the exception in "init" or will it also be
caught by the new_handler?