Reputation: 1775
In my in-house logging library, I am trying to change the custom exception class to derive from boost::exception instead of std::exception. I am doing this so that I can use a single catch block for both boost exceptions and my application exceptions. But I am running into an issue while logging it.
While logging the exception using boost::diagnostic_information(), I get the whole 9 yards about the throw location. This information is redundant for me since my custom class already collects and uses this info in a way I want. I don't want to print source code file/line/function info in the log.
If I define BOOST_EXCEPTION_DISABLE or don't use BOOST_THROW_EXCEPTION, it prints "Throw location unknown (consider using BOOST_THROW_EXCEPTION)" every time I log the exception.
But how do I escape this nagging?
Upvotes: 1
Views: 1817
Reputation: 1775
Alright, I think I found the answer myself. First of all, I don't have to derive my class from boost::exception, I can still continue to derive from std::exception. However, I should throw my std::exception derived class using BOOST_THROW_EXCEPTION. So it becomes boost::exception as it takes off. :-)
In between, I can add more info if required by catching and rethrowing.
typedef boost::error_info<struct tag_errmsg, std::string> exceptionInfo;
catch (boost::exception &e)
{
e << exceptionInfo("some more exception data");
throw;
}
And then I can finally catch it and print it this way:
catch (boost::exception &e)
{
std::exception const * se = dynamic_cast<std::exception const *>(&e);
if(se)
{
// will enter here only for my application exception and not for pure boost exception
std::cout << "STD: " << se->what();
}
std::cout << " BOOST: " << *boost::get_error_info<exceptionInfo>(e); }
}
This way I will get both the what() string from std::exception and the error message from boost::exception. Am I on the right track?
Upvotes: 4
Reputation: 1984
Do not you want to use exception::what() instead of boost::diagnostic_information()?
Upvotes: 0