Alex Gidan
Alex Gidan

Reputation: 2679

Weird issue with catching trivial boost exception

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!

Upvotes: 3

Views: 333

Answers (1)

Sebastian Redl
Sebastian Redl

Reputation: 71959

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.

Upvotes: 3

Related Questions