catch(...)能捕获所有异常,常用于程序顶层或线程入口作为最后防线,确保未处理异常时仍可执行清理和日志记录;应避免滥用,不可吞噬异常,推荐结合C++11的std::exception_ptr和std::rethrow_exception保留异常信息,或使用std::nested_exception构建异常链以传递上下文,提升错误诊断与处理能力。

C++中,
catch(...)
使用
catch(...)
try
catch
catch(...)
#include <iostream>
#include <string>
#include <stdexcept> // 包含一些标准异常类型
void mightThrowAnything(int type) {
if (type == 1) {
throw std::runtime_error("这是一个运行时错误!");
} else if (type == 2) {
throw 123; // 抛出整型异常
} else if (type == 3) {
throw std::string("这是一个字符串异常!");
} else {
// 模拟更不可预测的情况,比如内存分配失败等
// 这里只是一个示意,实际中可能更复杂
struct CustomException {};
throw CustomException();
}
}
int main() {
std::cout << "尝试捕获各种异常...\n";
// 场景1:捕获标准库异常
try {
mightThrowAnything(1);
} catch (const std::exception& e) {
std::cerr << "捕获到标准异常: " << e.what() << std::endl;
} catch (...) {
std::cerr << "捕获到未知异常 (场景1)\n";
}
std::cout << "\n";
// 场景2:捕获非标准异常(整型)
try {
mightThrowAnything(2);
} catch (int e) {
std::cerr << "捕获到整型异常: " << e << std::endl;
} catch (...) {
std::cerr << "捕获到未知异常 (场景2)\n";
}
std::cout << "\n";
// 场景3:捕获非标准异常(字符串)
try {
mightThrowAnything(3);
} catch (const std::string& e) {
std::cerr << "捕获到字符串异常: " << e << std::endl;
} catch (...) {
std::cerr << "捕获到未知异常 (场景3)\n";
}
std::cout << "\n";
// 场景4:直接使用catch(...)捕获所有
try {
mightThrowAnything(4); // 抛出 CustomException
} catch (...) {
std::cerr << "捕获到未知异常 (场景4),可能是自定义类型或其他未预料到的错误。\n";
// 在这里,我们无法知道异常的具体类型或内容。
// 通常会记录日志,然后进行一些通用的清理或程序退出。
}
std::cout << "\n程序继续执行。\n";
return 0;
}在上述代码中,
main
catch(...)
catch
catch (const std::exception& e)
catch (int e)
catch(...)
catch(...)
在我看来,
catch(...)
立即学习“C++免费学习笔记(深入)”;
实际应用场景:
catch(...)
main()
catch(...)
catch(...)
catch(...)
catch(...)
最佳实践:
catch(...)
catch(...)
catch
catch(...)
catch(...)
std::current_exception
std::rethrow_exception
throw;
catch(...)
虽然
catch(...)
潜在问题和局限性:
catch(...)
std::bad_alloc
std::logic_error
MyNetworkError
catch(...)
catch(...)
catch(...)
noexcept
noexcept
noexcept
std::terminate()
catch(...)
catch(...)
noexcept
catch(...)
throw;
C++11以及后续标准为我们处理异常,特别是那些“未知”或需要跨越不同上下文的异常,提供了更强大、更优雅的工具。这极大地改善了
catch(...)
std::exception_ptr
std::current_exception
std::current_exception()
catch
std::exception_ptr
std::exception_ptr
std::rethrow_exception(std::exception_ptr)
std::exception_ptr
这个组合的意义在于,你可以在
catch(...)
std::exception_ptr
#include <iostream>
#include <string>
#include <stdexcept>
#include <exception> // 包含 std::exception_ptr, std::current_exception, std::rethrow_exception
void mightThrowSomethingElse(int type) {
if (type == 1) {
throw std::runtime_error("这是另一个运行时错误!");
} else {
throw "一个C风格字符串异常!"; // 抛出C风格字符串
}
}
void errorLogger(std::exception_ptr ep) {
try {
if (ep) {
std::rethrow_exception(ep); // 重新抛出异常以获取其类型和内容
}
} catch (const std::exception& e) {
std::cerr << "日志记录器捕获到标准异常: " << e.what() << std::endl;
} catch (const char* msg) {
std::cerr << "日志记录器捕获到C风格字符串异常: " << msg << std::endl;
} catch (...) {
std::cerr << "日志记录器捕获到未知异常。\n";
}
}
int main() {
std::cout << "使用 C++11+ 机制处理异常...\n";
try {
mightThrowSomethingElse(2); // 抛出C风格字符串
} catch (...) {
std::cerr << "主函数捕获到未知异常,准备记录日志并重新处理。\n";
std::exception_ptr ep = std::current_exception(); // 捕获当前异常
errorLogger(ep); // 将异常指针传递给日志记录器
// 此时可以决定是否再次 rethrow_exception(ep) 或做其他处理
}
std::cout << "\n程序继续执行。\n";
return 0;
}通过这种方式,即使在
catch(...)
std::nested_exception
std::throw_with_nested
#include <exception>
#include <iostream>
#include <stdexcept>
// 假设这是底层函数,可能抛出异常
void readFile() {
throw std::runtime_error("文件读取失败: 权限不足");
}
// 假设这是高层函数,调用底层函数
void loadData() {
try {
readFile();
} catch (...) {
// 捕获底层异常,然后抛出带有嵌套异常的新异常
std::throw_with_nested(std::runtime_error("数据加载失败"));
}
}
// 处理嵌套异常的辅助函数
void handleNested(const std::exception& e) {
std::cerr << "处理异常: " << e.what() << std::endl;
try {
std::rethrow_if_nested(e); // 如果有嵌套异常,则重新抛出
} catch (const std::exception& nested_e) {
std::cerr << " 嵌套异常: " << nested_e.what() << std::endl;
handleNested(nested_e); // 递归处理嵌套异常
}
}
int main() {
try {
loadData();
} catch (const std::exception& e) {
handleNested(e);
}
return 0;
}std::throw_with_nested
catch(...)
总而言之,C++11及更高版本提供了工具,让
catch(...)
以上就是C++如何使用catch(...)捕获所有异常的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号