問題描述
是否有 Java 的 C++ 等價物
Is there a c++ equivalent of Java's
try {
...
}
catch (Throwable t) {
...
}
我正在嘗試調試調用本機 Windows 函數的 Java/jni 代碼,但虛擬機不斷崩潰.本機代碼在單元測試中看起來很好,只有在通過 jni 調用時才會崩潰.通用的異常捕獲機制將證明非常有用.
I am trying to debug Java/jni code that calls native windows functions and the virtual machine keeps crashing. The native code appears fine in unit testing and only seems to crash when called through jni. A generic exception catching mechanism would prove extremely useful.
推薦答案
try{
// ...
} catch (...) {
// ...
}
將捕獲所有 C++ 異常,但它應該被認為是糟糕的設計.您可以使用 c++11 的新 current_exception 機制,但是如果您沒有能力使用 c++11(需要重寫的遺留代碼系統),那么您就沒有用于獲取消息或名稱的命名異常指針.您可能希望為您可以捕獲的各種異常添加單獨的 catch 子句,并且只捕獲底部的所有內容以記錄意外異常.例如:
will catch all C++ exceptions, but it should be considered bad design. You can use c++11's new current_exception mechanism, but if you don't have the ability to use c++11 (legacy code systems requiring a rewrite), then you have no named exception pointer to use to get a message or name. You may want to add separate catch clauses for the various exceptions you can catch, and only catch everything at the bottom to record an unexpected exception. E.g.:
try{
// ...
} catch (const std::exception& ex) {
// ...
} catch (const std::string& ex) {
// ...
} catch (...) {
// ...
}
這篇關于C++ 捕獲所有異常的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!