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


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
> (
> 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;
> and
> 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, gregod at, cpdaniel at, john at