打印 boost::exception 数据的测试程序
在 C++ 编程中,错误处理是一个关键的问题。程序中出现错误时,如果能够清晰明了地定位错误原因,就可以更快地解决问题。而 boost::exception 这个库就提供了一种方便、灵活的异常处理方式。在程序中,我们可以将任意类型的数据添加到 boost::exception 中,然后在捕获异常时输出这些数据,以协助程序员更快地定位问题。
下面是一个简单的例子,演示了如何将一个字符串和一个整数添加到 boost::exception 中,并在捕获异常时输出这些数据。代码中使用了 boost 库中的 boost::current_exception、boost::diagnostic_information 和 boost::exception 对象等类和函数。
#include <boost/exception/all.hpp>
#include <iostream>
#include <stdexcept>
int main() {
try {
std::string message = "A test exception occurred.";
int error_code = 42;
// 抛出一个带有 string 和 int 类型数据的异常
BOOST_THROW_EXCEPTION(boost::enabl