boost::exception-如何打印详细信息

boost::exception - how to print details?

本文关键字:打印 详细信息 何打印 exception- boost      更新时间:2023-10-16

我的程序中有这样的代码:

catch (boost::exception& ex) {
    // error handling
}

如何打印详细信息?错误消息、堆栈跟踪等。?

对于像boost::exception这样通用的东西,我认为您正在寻找boost::diagnostic_information函数来获得更好的字符串表示。

#include <boost/exception/diagnostic_information.hpp>
catch (const boost::exception& ex) {
    // error handling
    std::string info = boost::diagnostic_information(ex);
    log_exception(info); // some logging function you have
}

要获取异常的堆栈,我将从StackOverflow问题C++显示异常的堆栈跟踪开始。

您可以使用boost::diagnostic_information()来获取实际的错误消息和异常的来源。即

catch (const boost::exception& ex) {
    // error handling
    std::cerr << boost::diagnostic_information(ex);
}