开发者

When to catch boost::bad_lexical_cast

开发者 https://www.devze.com 2023-01-05 23:18 出处:网络
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 shoul

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 开发者_如何学Cthe 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号