繁体   English   中英

C ++禁用堆栈帧下面的异常

[英]C++ disable exceptions below stack frame

有没有办法使异常不在特定堆栈帧之上传播,同时不丢失堆栈信息?

IE浏览器,

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

这将阻止你的投掷电话

不同操作系统中的堆栈跟踪功能

backtrace_symbols(3) - linux,mac osx

CaptureStackBackTrace(...) - windows

现场演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM