我试图检查有多少递归导致堆栈溢出。 但是为什么下面的代码没有输出任何东西呢?
#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;
}
最佳答案
因为在大多数实现中,堆栈溢出会导致操作系统彻底终止进程,而不是进程抛出异常。
C++
标准没有指定堆栈溢出导致的预期行为。堆栈溢出的结果是不确定的。在这种情况下,特定的 C++
实现当然可以抛出异常,但这不是必需的。
关于C++:从堆栈溢出中获取异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37095299/