Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::exception - how to print details?

Tags:

c++

boost

I have such codein my program:

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

How can I print details? Error message, stacktrace etc.?

like image 734
Oleg Vazhnev Avatar asked Nov 29 '13 10:11

Oleg Vazhnev


2 Answers

For something as generic as a boost::exception, I think you are looking for the boost::diagnostic_information function to get a niceish string representation.

#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
}

To get the stack for an exception, I'd start with the StackOverflow question C++ display stack trace on exception.

like image 191
chwarr Avatar answered Nov 06 '22 16:11

chwarr


You can use boost::diagnostic_information() to get the actual error messages and origin of the exception. i.e.

catch (const boost::exception& ex) {
    // error handling
    std::cerr << boost::diagnostic_information(ex);
}
like image 37
Constantin Avatar answered Nov 06 '22 17:11

Constantin