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 
}

相关文章