下面long型的rang明显要小于,unsigned long为什么还抛出异常?

有一个题目是bitset 提供了成员函数,to_ulong,如果bitset提供了超出unsigned long的位数长度时,抛出一个over_flow的异常,编写这样的程序.我做的结果如下:

#include <iostream>
#include <vector>
#include <stdexcept>
#include <bitset>
int main()
{
unsigned long long _Initiliztion_Ibitset;

std::bitset<32> _Varible_Bitset(_Initiliztion_Ibitset);

try {
_Varible_Bitset.to_ullong();

throwstd::overflow_error("The varible rang was more than parameter tha system default");

}
catch (std::overflow_error Error)
{

std::cout<<Error.what();

system("pause");
}

return0;
}
这个 运行起来 抛出了异常 可是下面的 怎么还抛出异常:
#include <iostream>
#include <vector>
#include <stdexcept>
#include <bitset>
int main()
{
long _Initiliztion_Ibitset;//这里的long型的rang明显要小于,unsigned long 怎么还抛出异常

std::bitset<32> _Varible_Bitset(_Initiliztion_Ibitset);

try {
_Varible_Bitset.to_ullong();

throwstd::overflow_error("The varible rang was more than parameter tha system default");

}
catch (std::overflow_error Error)
{

std::cout<<Error.what();

system("pause");
}

return0;
}

蝴蝶刀刀
浏览 103回答 2
2回答

子衿沉夜

try{//必须执行的语句}catch(){//try里面出现异常则执行这里}这句话一般用做程序抛出异常.防止程序出现卡死的情况.我们一般用此语句主要用在对数据库的增删改的敌方.毕竟这是最容易出错的敌方.为了使程序提高性能,不崩溃的情况下.就是要使用这句语句.

九州编程

你自己抛出的异常啊:throwstd::overflow_error("The varible rang was more than parameter tha system default");然后你自己又捕获了异常.也就是你捕获的异常不是_Varible_Bitset.to_ullong();抛出的, 这句代码本身也不会抛出异常
打开App,查看更多内容
随时随地看视频慕课网APP