9

I have such codein my program:

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

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

Oleg Vazhnev
  • 22,231
  • 49
  • 161
  • 290
  • Well, judging from the documentation, `boost::exception`, which is meant to be a base class, doesn't seem to have any actual exception information. Did you have a look at `boost::get_error_info`? I have no idea if that'll help. You could also check out `boost::diagnostic_information`. I only used the `std::exception`s so far. – thokra Nov 29 '13 at 10:25

2 Answers2

6

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);
}
RiaD
  • 45,211
  • 10
  • 74
  • 119
Constantin
  • 8,351
  • 12
  • 74
  • 119
6

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.

chwarr
  • 6,370
  • 1
  • 27
  • 55