簡體   English   中英

如何捕獲任何 C++ 標准異常?

[英]How to catch any c++ standard exception?

我知道在 C++ 中,您可以使用以下方法捕獲任何數據類型的異常:

try {
  // throw exception here
} catch (...) {
  // handle exception here
}

但我想捕獲任何 C++ 標准異常,例如std::logic_errorstd::out_of_range std::logic_error ,而不是其他數據類型,例如stringint 如何僅捕獲 C++ 標准異常? 我想對傳入的 C++ 標准異常對象調用exp.what() ,而使用上面的代碼是不可能的。

所有標准異常都源自std::exception ,因此請改為捕獲它:

try {
    // throw exception here
}
catch (const std::exception &e) {
    // handle exception here
}

暫無
暫無

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

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