views:

66

answers:

1

From the documentation on the boost site for lexical cast it states that when converting from a numeric type to a string type the conversion can throw a bad_lexical_cast. Clearly in this case we should always handle this exception should it be thrown.

My question is, what about the other way around, going from a numeric type to a string? This is less risky on operation but it does not state on the boost documentation whether this operation can throw a bad_lexical_cast although the example given ommits the catch block.

log_message("Error " + boost::lexical_cast<std::string>(yoko) + ": " + strerror(yoko));

Should I be catching a potential exception when converting from a numeric type to a string?

+2  A: 

As far as I know, there is no scenario in which an inbuilt numeric type can fail to be expressed in a string.

DeadMG
There is however a case where the conversion can fail - the string's memory allocation could throw.
anon
That wouldn't be caught and re-thrown as a boost::bad_lexical_cast though?
jon hanson
It would be thrown as a std::bad_alloc. Boost might internally catch it and rethrow as something else, but I doubt a bad_lexical_cast.
DeadMG
It shouldn't, and I doubt it does. It didn't when I last looked.
MSalters
It doesn't catch it, I just checked.
Konrad
Wasn't suggesting that it does/should catch it, more that if it did, it would never rethrow as a bad_lexical_cast.
DeadMG