簡體   English   中英

std :: exception沒被捕獲?

[英]std::exception not being caught?

這個對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");
}

“我究竟做錯了什么?”

那么,關於所謂的例外的不出現,我沒有提出任何假設。

但是有些事情你做錯了:

  • 將字符串傳遞給std::exception構造函數。 這是一個非標准的擴展; std::exception沒有帶字符串參數的構造函數。 如果要傳遞異常文本,請使用std::runtime_error

  • 在可能的拋出之后沒有正常的聲明意味着您無法確定拋出異常。

  • 在異常消息中有換行符。 非常有問題的慣例。 其他來源的例外情況不會有最終的新行。

  • 報告stdout錯誤。 為此使用stderr 這就是它的用途。

  • 釋放catch子句中的資源。 一般來說,造成野蠻的混亂。 不要,為此使用析構函數。

你怎么知道它沒被抓住?

我猜它實際上是由catch( std::exception const& err)子句catch( std::exception const& err) 但是你忘了在打印字符串的末尾添加\\n ,因此它不會立即出現在行緩沖輸出設備上。 你的代碼中的所有其他fprintf都有\\n ,但這個特定的沒有。

\\n或輸出添加到stderr

你可能希望這個fprintf最終看起來像

fprintf(stdout, "Exception Found: %s", err.what());

意味着異常文本中的\\n應該用作終止\\n 如果是這樣,那就這樣做吧。 但是你現在所擁有的東西不會立即出現在屏幕上,導致你相信異常沒有被捕獲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM