如何捕获任何 C++ 标准异常?

How to catch any c++ standard exception?

我知道在 C++ 中,您可以使用以下方法捕获任何数据类型的异常:

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

但我想捕获任何 C++ 标准异常,例如 std::logic_errorstd::out_of_range,而不是其他数据类型的异常,例如 stringint。我怎样才能只捕获 C++ 标准异常?我想在传入的 C++ 标准异常对象上调用 exp.what(),使用上面的代码是不可能的。

所有标准异常都派生自 std::exception,因此请捕获它:

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