Weird issue with catching trivial boost exception

≯℡__Kan透↙ 提交于 2019-12-07 17:20:20

问题


I have an issue with the following straightforward code:

    try
    {
        boost::lexical_cast<unsigned int>("invalid string");
    }
    catch(::boost::bad_lexical_cast &)
    {
        //It's not catched!!!
        return;
    }
    catch (std::exception &e){
        std::cerr << boost::diagnostic_information(e) << std::endl;

        ::boost::bad_lexical_cast s;
        std::string ss = typeid(s).name();
        std::cout << "'" << s.what()<<"': '"<< ss <<"'";

        std::string ee = typeid(e).name();
        std::cout << "'" << e.what()<<"': '"<< ee <<"'";
    }

The boost::bad_lexical_cast exception thrown by lexical_cast is somehow different than the one I try to catch, so the first catch is simply ignored. One exception is of type:

(mangled) N5boost16exception_detail10clone_implINS0_19error_info_injectorINS_16bad_lexical_castEEEEE

that is a:

boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >

while the other one simply boost::bad_lexical_exception.

My question is: how can I better debug such a situation? Why does it happen only locally while n another environment the issue doesn't appear? And how can I prevent these weird behaviours?

Thank for your help!


回答1:


The weird type you see is a boost::exception_detail::clone_impl< boost::exception_detail::error_info_injector< boost::bad_lexical_cast>>. It's a wrapper around (and derived from) bad_lexical_cast provided by Boost.Exception, which provides support for boost::exception_ptr and the error info facilities. It should be caught just fine by the first catch.

When it isn't, that's usually an effect of conflicting RTTI information in different dynamic libraries. It's the only thing I can think of to explain the behavior of your test case.



来源:https://stackoverflow.com/questions/28603899/weird-issue-with-catching-trivial-boost-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!