boost lexical_cast异常捕获

chuengchuenghq 2010-07-06 10:03:47

try
{
string strTmpVar = "-11";
unsigned long aaa = boost::lexical_cast<unsigned long>(strTmpVar);
}
catch(boost::bad_lexical_cast& e) {
// bad lexical cast: source type value could not be interpreted as target
std::cout << e.what() << std::endl;
int aa = -1;
}
catch(std::bad_cast&e)
{
int aa = 1;
}


上面的异常为什么没被捕获?把一个负数cast成unsigned long应该要抛出异常的啊?
...全文
495 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
CyberLogix 2011-05-22
  • 打赏
  • 举报
回复
是啊,隐式转换通常比较隐蔽
cattycat 2010-07-07
  • 打赏
  • 举报
回复
lexical_cast的文档中没有提到会发生隐式转换,如果你的代码真没catch到异常,那就是发生隐式转换了。
cattycat 2010-07-07
  • 打赏
  • 举报
回复
我看了一下boost的文档,老邓和我看的一样。
numeric_cast 可以在转换一个数字类型时检查范围的损失,如果范围不能被保持就抛出一个异常。当在numeric和numeric之间转换的时候,numeric_cast 可能提供了比lexical_cast更好的行为。

Lexical_cast函数模版为以文本表示的任意的类型之间的转换提供了方便和一致的形式。它提供的简化形式是在表达式级别上的简单性易用性。对于更多相关的转换,比如比lexical_cast提供的默认行为更为精确的或者需要更严密的格式转换控制,这时推荐传统的stringstream。

所以如果你想达到想要的异常,用numeric_cast更好,你说你的输入是string,那估计得用stringstream了。
taodm 2010-07-07
  • 打赏
  • 举报
回复
楼主试试
istringstream in("-1");
in >> aaa;
cout << aaa;
sscanf("-1", "%ul", &aaa);
cout << aaa;
吧,超好玩。
linsen_519 2010-07-06
  • 打赏
  • 举报
回复
catch(std::bad_cast&e)
{
int aa = 1;
}
有没有在这里捕获你知道吗?
taodm 2010-07-06
  • 打赏
  • 举报
回复
为啥我用楼主的原始代码,跑异常了,并且被捕获了呢。
chuengchuenghq 2010-07-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 loaden 的回复:]
查了下boost的文档,我觉得楼主可以改用:numeric_cast
http://www.boost.org/doc/libs/1_43_0/libs/numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__.html
C/C++ code
int main()
{
using boost……
[/Quote]

问题是我现在是输入一个string,我需要去CHECK这个string
然后在用numeric_cast,numeric_cast的参数是一个数字
「已注销」 2010-07-06
  • 打赏
  • 举报
回复
查了下boost的文档,我觉得楼主可以改用:numeric_cast
http://www.boost.org/doc/libs/1_43_0/libs/numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__.html
int main()
{
using boost::numeric_cast;

using boost::numeric::bad_numeric_cast;
using boost::numeric::positive_overflow;
using boost::numeric::negative_overflow;

try
{
int i=42;
short s=numeric_cast<short>(i); // This conversion succeeds (is in range)
}
catch(negative_overflow& e) {
std::cout << e.what();
}
catch(positive_overflow& e) {
std::cout << e.what();
}

try
{
float f=-42.1234;

// This will cause a boost::numeric::negative_overflow exception to be thrown
unsigned int i=numeric_cast<unsigned int>(f);
}
catch(bad_numeric_cast& e) {
std::cout << e.what();
}

double d= f + numeric_cast<double>(123); // int -> double

unsigned long l=std::numeric_limits<unsigned long>::max();

try
{
// This will cause a boost::numeric::positive_overflow exception to be thrown
// NOTE: *operations* on unsigned integral types cannot cause overflow
// but *conversions* to a signed type ARE range checked by numeric_cast.

unsigned char c=numeric_cast<unsigned char>(l);
}
catch(positive_overflow& e) {
std::cout << e.what();
}


return 0;
}
「已注销」 2010-07-06
  • 打赏
  • 举报
回复
而如果你改成这样:
string strTmpVar = "-11a";

因为这已经不再是纯数字了,后面的a必须会导致其抛出异常。
「已注销」 2010-07-06
  • 打赏
  • 举报
回复
我想这里应该是一个隐式转换吧。
就像:unsigned long a = -11;一样,我想你如果输出aaa的值的话,应该和a的值是一样的。
Janear 2010-07-06
  • 打赏
  • 举报
回复
学习···
  • 打赏
  • 举报
回复
"-11"改成"a" 试试
「已注销」 2010-07-06
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 chuengchuenghq 的回复:]

引用 7 楼 taodm 的回复:

为啥我用楼主的原始代码,跑异常了,并且被捕获了呢。

可能是我的BOOST 库太老啦!!!
[/Quote]
我特意试了一下,事实上,我用最新的1.43,加上MinGW 4.4.4,并不抛出异常。
因为看起来它并没有溢出异常的处理。

不知道你有没有看到我在5楼的建议?
chuengchuenghq 2010-07-06
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 taodm 的回复:]

为啥我用楼主的原始代码,跑异常了,并且被捕获了呢。
[/Quote]
可能是我的BOOST 库太老啦!!!

65,182

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧