在下面的代码中,我想获得what()boost :: exception 的消息.
#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;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我收到消息:
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> >
Run Code Online (Sandbox Code Playgroud)
但是当我没有捕获异常时,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
Run Code Online (Sandbox Code Playgroud)
我想要那个消息:bad lexical cast: source type value could not be interpreted as target; 但我找不到拥有它的方法.提升异常系统对我来说是一个谜.
如何得到这个消息?
编辑:boost :: exception没有what()方法.那么,如何写shell std::exception::what: bad lexical cast: source type value could not be interpreted as target,因为这不是一个std::exception?
抓住它的bad_lexical_cast使用方法what():
catch (const boost::bad_lexical_cast& e)
{ // ^^^^^^^^^^^^^^^^^^^^^^^
std::cout << "Exception: " << e.what() << "\n";
// ^^^^^^^^
}
Run Code Online (Sandbox Code Playgroud)
并且会显示Exception: bad lexical cast: source type value could not be interpreted as target
| 归档时间: |
|
| 查看次数: |
2431 次 |
| 最近记录: |