在下面的代码中,我希望获得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::异常没有方法。那么,它怎么能写std::exception::what: bad lexical cast: source type value could not be interpreted as target
,因为这不是std::exception
?
发布于 2016-04-21 10:36:25
将其捕获为bad_lexical_cast
,以使用what()
方法
catch (const boost::bad_lexical_cast& e)
{ // ^^^^^^^^^^^^^^^^^^^^^^^
std::cout << "Exception: " << e.what() << "\n";
// ^^^^^^^^
}
它将显示Exception: bad lexical cast: source type value could not be interpreted as target
https://stackoverflow.com/questions/36766740
复制相似问题