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(...) 没有捕获异常,我的程序仍然崩溃
基础教程推荐
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01