boost :: exception - 如何打印细节?

jav*_*red 8 c++ boost

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

catch (boost::exception& ex) {
    // error handling
}
Run Code Online (Sandbox Code Playgroud)

我该如何打印细节?错误信息,堆栈跟踪等?

Con*_*tin 6

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

catch (const boost::exception& ex) {
    // error handling
    std::cerr << boost::diagnostic_information(ex);
}
Run Code Online (Sandbox Code Playgroud)


chw*_*arr 5

对于像 a 这样通用的东西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
}
Run Code Online (Sandbox Code Playgroud)

要获取异常的堆栈,我将从 StackOverflow 问题C++ display stack trace on exception 开始