【发布时间】:2016-04-21 10:25:02
【问题描述】:
在下面的代码中,我想得到一个 boost::exception 的 what() 消息。
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/exception/diagnostic_information.hpp>
int main(void)
{
try
{
int i(boost::lexical_cast<int>("42X"));
}
catch (boost::exception const &e)
{
std::cout << "Exception: " << boost::diagnostic_information_what(e) << "\n";
}
return 0;
}
当我运行它时,我会收到消息:
Exception: Throw location unknown (consider using BOOST_THROW_EXCEPTION)
Dynamic exception type: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >
但是当我没有捕捉到异常时,shell 输出:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >'
what(): bad lexical cast: source type value could not be interpreted as target
[1] 8744 abort ./a.out
我想要那个消息:bad lexical cast: source type value could not be interpreted as target;但我找不到获得它的方法。 boost异常系统对我来说是个谜。
如何得到这个消息?
编辑:boost::exception 没有what() 方法。那么,shell 怎么写std::exception::what: bad lexical cast: source type value could not be interpreted as target,因为这不是std::exception?
【问题讨论】:
-
自从 c++11 的嵌套异常出现以来,我找不到使用 boost::exception 的有力案例。以标准方式进行是否为时已晚? en.cppreference.com/w/cpp/error/throw_with_nested