Reputation: 2498
I tried to check how many recursions causes stack overflow. But why doesn't the following code output anything?
#include <iostream>
#include <exception>
int counter = 0;
void endlessloop() try {
++counter;
endlessloop();
} catch (std::exception &e) {
std::cout << "Call stack overflew. Counter: " << counter << std::endl <<
"Exception: " << e.what() << std::endl;
}
int main() {
endlessloop();
return 0;
}
Upvotes: 1
Views: 76
Reputation: 118340
Because on most implementations, overflowing the stack results in the operating system killing the process outright, instead of the process throwing an exception.
The C++
standard does not specify the expected behavior that results from a stack overflow. The results of a stack overflow are unspecified. A particular C++
implementation can certainly throw an exception, in this situation, but it is not required to do that.
Upvotes: 4