lexical_cast int to string

会有一股神秘感。 提交于 2019-12-21 07:19:11

问题


Is it safe to ignore exception of boost::lexical_cast when converting int to std::string?


回答1:


Exception raised by lexical cast when converting an int to std::string are not associated to the conversion, but to resource unavailable. So you can ignore this in the same way you ignore the exception bad_alloc raised by operator new.




回答2:


As you say, I don't believe the cast can fail for the numerical types for conversion reasons - it can still fail because the string cannot be allocated, of course, but people don't normally catch that error except at the highest level of their code.




回答3:


If you "ignore" an exception it will propagate back up the call stack until it is caught elsewhere, or it terminates the program, the point being you can safely not catch exceptions without worrying about you program continuing and doing unsafe/unknown things (as long as a "crash" to command prompt is acceptable error behaviour or you have some other way of dealing with unknown exceptions).

Unfortunately exception stack traces aren't so easy to get in C++, so creating useful error messages when exceptions aren't caught locally isn't always easy.



来源:https://stackoverflow.com/questions/2736060/lexical-cast-int-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!