奇怪的消息 (_Base_bitset::_M_do_to_ulong) 从溢出异常处理程序中打印出来
Strange message (_Base_bitset::_M_do_to_ulong) getting printed out of overflow exception handler
我正在尝试异常处理,这段代码并没有抛出它想要的字符串:
bitset<134> b;
b.set();
try{
if(!b.to_ulong())
throw std::overflow_error("Overflow occuredn");
}
catch(std::overflow_error& e)
{
cout<<e.what();
}
输出为:
_Base_bitset::_M_do_to_ulong
而当我用这个替换if条件时
if(3>2)
打印预期的字符串。这是怎么回事?
怎么回事:如果位域不适合unsigned long
,b.to_ulong();
会在您有机会之前抛出自己的消息std::overflow_error
。if
永远不会经过测试。
无论如何,if
测试注定要失败。它将对b.to_ulong()
返回的任何非零值引发异常。不是你想要的。
我能想到的获取消息的最简单方法是捕获to_ulong
抛出的异常并抛出您自己的异常。
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset < 134 > b;
b.set();
try
{
b.to_ulong();
}
catch (std::overflow_error& e)
{
throw std::overflow_error("Overflow occuredn");
}
}
自己执行测试可能比调用to_ulong
并处理两个异常更快。
在你的代码中:
bitset<134> b;
b.set(); // all bits in the set are 1's
try
{
if (!b.to_ulong()) // throws its own std::overflow_error()!
throw std::overflow_error("Overflow occuredn"); // doesn't reach.
}
catch(std::overflow_error& e)
{
cout << e.what(); // catches the exception thrown by b.to_ulong()
}
相关文章:
- 与异常处理程序中的操作员<<不匹配
- 数组 C++ 上的异常处理程序
- 视觉 std::矢量无异常:警告 C4530:使用了C++异常处理程序,但未启用展开语义.指定 /EHsc
- 奇怪的消息 (_Base_bitset::_M_do_to_ulong) 从溢出异常处理程序中打印出来
- C++ 中未处理的异常处理程序
- 如何在 Windows 异常处理程序中设置 MMX 寄存器以模拟不受支持的 3DNow! 说明
- G++ 自定义异常处理程序
- 内部异常处理程序,MessageBox()成功返回IDOK,但从未显示
- 如何从将触发脚本异常处理程序的C++引发异常
- 将 INT3 中断保留给视觉工作室上的应用程序异常处理程序
- 如何在气泡排序中为字符串和双精度实现异常处理程序
- 从硬件异常处理程序引发C++异常.为什么 -fnon-call-exception 没有按预期运行
- 我应该如何编写国际化的异常处理程序
- 使用try-Catch异常处理程序和if-else条件检查之间的区别
- 异常处理程序
- 我可以从Qt中的异常处理程序中发出信号吗
- c++异常处理程序中嵌套的try..catch
- 结构化异常处理程序(SEH)不会捕获堆损坏
- MSVC++6.0:C1509“函数中的异常处理程序状态太多”
- . net异常处理程序导致Visual c++ 6.0异常上的堆栈溢出