有没有办法使异常不传播到特定堆栈帧之上,同时又不丢失堆栈信息?
浏览器
int foo() {
throw 3;
}
int bar() {
// do something here
foo();
}
int main() {
try {
bar();
} catch(...) {
std::cout << "Caught";
}
}
我希望它在“throw 3”调用时终止,而不会被 main 捕获。
这可能吗?
最佳答案
只需在函数声明和定义之后添加throw()
#include <iostream>
void* g_pStackTrace = NULL;
int foo() throw();
int foo() throw() {
g_pStackTrace = <stack_trace_function_call>;
throw 3;
}
int bar() {
// do something here
foo();
return 0;
}
int main() {
bar();
if (g_pStackTrace != NULL)
{
// Work with our stack
}
}
这会阻止你的 throw 调用
不同操作系统中的堆栈跟踪功能
backtrace_symbols(3)
- linux, mac osx
CaptureStackBackTrace(...)
- 窗口
关于C++ 禁用堆栈帧下方的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33180607/