1

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;
}

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.