Boost logo

Boost :

Subject: Re: [boost] [lexical_cast] lexical_cast does not throw bad_lexical_cast
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-09-15 12:55:36


AMDG

Carlos Rafael Giani wrote:
> I've noticed that sometimes lexical_cast does not throw
> bad_lexical_cast. Instead, it throws
> 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast>
> >'. A catch clause expecting a bad_lexical_cast fails here.

This type ought to inherit from boost::bad_lexical_cast, so you
should be able to catch bad_lexical_cast.

> What seems confusing to me is that the lexical_cast docs do mention
> "catch (bad_lexical_cast &)" as correct code
> (http://www.boost.org/doc/libs/1_39_0/libs/conversion/lexical_cast.htm).
> The solution that worked for me was to catch boost::exception instead.
>
> Am I missing something here? I consulted the documentation, but still
> have no answer. It also seems that I am not alone with this problem;
>
> http://www.nabble.com/Porting-from-boost_1_33-to-boost_1_36-exception-td19708697.html
> and
> http://archives.free.net.ph/message/20090715.183116.374b4777.en.html
>
> at least indicate this for me.

It works for me with msvc 9.0. Do you have a test case?

#include <boost/lexical_cast.hpp>
#include <iostream>

int main() {
    try {
        int i = boost::lexical_cast<int>("123ee");
    } catch(boost::bad_lexical_cast&) {
        std::cout << "okay" << std::endl;
    } catch(...) {
        std::cout << "???" << std::endl;
    }
}

In Christ,
Steven Watanabe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk