c++ - 如何捕捉任何 C++ 标准异常?
问题描述
我知道在 C++ 中,您可以使用以下方法捕获任何数据类型的异常:
try {
// throw exception here
} catch (...) {
// handle exception here
}
但我想捕获任何 C++ 标准异常,例如std::logic_error
, std::out_of_range
,而不是其他数据类型的异常,例如string
or int
。如何仅捕获 C++ 标准异常?我想调用exp.what()
传入的 C++ 标准异常对象,而使用上面的代码是不可能的。
解决方案
所有标准异常都派生自std::exception
,因此请抓住它:
try {
// throw exception here
}
catch (const std::exception &e) {
// handle exception here
}
推荐阅读
- ansible - Ansible - 将字符串列表转换为具有预定义键的字典列表
- javascript - 将 chrome 扩展添加到 mac osx 的电子 js 应用程序?
- php - Elasticsearch 查找子域
- codenameone - 代号一 - 一次仅验证一个输入
- javascript - 如何为 Jasmine 中的特定功能编写规范?
- java - 在多个java版本JDK1.8和JDK10之间切换
- python - Python/Cython foo.py --> foo.so
- ios - 本地领域迁移不起作用 Swift
- google-ads-api - Google AdWords 问题:DEVELOPER_TOKEN_NOT_APPROVED
- r - 用 gsub 替换除某些字符串以外的字符