catch(...) is not catching an exception, my program is still crashing(catch(...) 没有捕获异常,我的程序仍然崩溃)
问题描述
我的应用程序在初始化时崩溃的测试仪出现问题.我添加了更多的日志记录和异常处理,但它仍然崩溃并显示通用的此程序已停止工作"消息,而不是触发我的错误处理.
I'm having a problem with a tester that my application crashes in initialization. I added more logging and exception handling but it still crashes with the generic "this program has stopped working" message rather than triggering my error handling.
鉴于我的 main() 看起来像这样并且有 catch(...)
在什么情况下不会触发?
Given my main() looks like this and has catch(...)
under what circumstances would this not be triggered?
try{
simed::CArmApp app(0, cmd);
for(bool done = false;!done;)
{
done = !app.frame();
}
} catch(const std::runtime_error &e){
handleApplicationError(e.what());
return -1;
} catch(...) {
handleApplicationError("Unknown Error");
return -999;
}
我的代码正在调用执行 OpenGL 渲染的库,我认为这是出了问题.
My code is calling into a library doing OpenGL rendering which is where I believe things are going wrong.
推荐答案
如果 C++ catch(...)
块没有捕获错误,可能是因为 Windows 错误.
If a C++ catch(...)
block is not catching errors maybe it is because of a Windows error.
在 Windows 上,有一个概念叫做 结构化异常处理,这是操作系统引发异常"的地方.当发生不好的事情时,例如取消引用无效的指针、除以零等.我说例外";因为这些不是 C++ 异常;相反,这些是 Windows 以 C 风格定义的严重错误 - 这是因为 Win32 是用 C 编写的,因此 C++ 异常不可行.
On Windows there is a concept called Structured Exception Handling which is where the OS raises "exceptions" when bad things happen such as dereferencing a pointer that is invalid, dividing by zero etc. I say "exceptions" because these are not C++ exceptions; rather these are critical errors that Windows defines in a C-style fashion - this is because Win32 was written in C so C++ exceptions were not viable.
另见:
- C++ 异常和结构化异常之间的区别
- try-except 声明
- 方法从
EXCEPTION_POINTERS
结构 获取堆栈跟踪
根据评论更新
如果您同时需要 C++ 异常处理和 SEH,也许您可以尝试以下(未经测试的)代码:
If you want both C++ exception handing and SEH perhaps you could try the following (untested) code:
__try
{
try
{
// Your code here...
}
catch (std::exception& e)
{
// C++ exception handling
}
}
__except(HandleStructuredException())
{
// SEH handling
}
这篇关于catch(...) 没有捕获异常,我的程序仍然崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:catch(...) 没有捕获异常,我的程序仍然崩溃


基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01