std::Exception 没有被捕获?

Jos*_*ias 2 c++ exception

对 glewInit() 的调用失败(据记录,我没有答案,但是......)并且它抛出异常。

不幸的是它没有被我的任何捕获物捕获,甚至没有(...)。

我究竟做错了什么?

try {

   // Initialize GLEW
    if (glewInit() != GLEW_OK)
        throw std::exception("Failed to initialize GLEW\n");


} catch ( std::system_error const& err) {
    fprintf(stdout, "System Error: %s", err.what());
    glfwTerminate(); // Free glfw if it has been allocated
    // Try Again
    this->InitWithSize(_width, _height);
} catch( std::exception const& err) {
    fprintf(stdout, "Exception Found: %s", err.what());
} catch ( ... ) {
    fprintf(stdout,"Unknown Exception Occured\n");
}
Run Code Online (Sandbox Code Playgroud)

Che*_*Alf 5

“我究竟做错了什么?”

好吧,关于所谓例外的不出现,我没有提出任何假设。

但这里有一些你做错了的事情:

  • 将字符串传递给std::exception构造函数。这是一个非标准的扩展;std::exception没有采用字符串参数的构造函数。如果您想传递异常文本,请使用std::runtime_error.

  • 在可能的抛出之后没有正常的语句意味着您无法确定异常是否被抛出。

  • 异常消息中有换行符。非常有问题的惯例。来自其他来源的例外不会有最后的换行符。

  • 报告 上的错误stdout。用于stderr此。这就是它的用途。

  • 释放条款中的资源catch。一般来说,会造成严重的混乱。不要,为此使用析构函数。