获取boost :: exception的what()消息

Fre*_*ios 6 c++ boost

在下面的代码中,我想获得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

And*_* DM 4

抓住它的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

  • 非常感谢。基类怎么可能不包含纯虚方法“what()”?为什么会做出这样的选择呢? (2认同)