C++:从堆栈溢出中获取异常?
C++: Getting exception from stack overflow?
我试图检查有多少递归导致堆栈溢出。
但是为什么下面的代码没有输出任何东西?
#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++
实现当然可以抛出异常,但不需要这样做。
我试图检查有多少递归导致堆栈溢出。 但是为什么下面的代码没有输出任何东西?
#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++
实现当然可以抛出异常,但不需要这样做。