有一個題目是bitset 提供了成員函數(shù),to_ulong,如果bitset提供了超出unsigned long的位數(shù)長度時,拋出一個over_flow的異常,編寫這樣的程序.我做的結(jié)果如下:#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;}
2 回答

子衿沉夜
TA貢獻1828條經(jīng)驗 獲得超3個贊
try
{
//必須執(zhí)行的語句
}
catch()
{
//try里面出現(xiàn)異常則執(zhí)行這里
}
這句話一般用做程序拋出異常.防止程序出現(xiàn)卡死的情況.我們一般用此語句主要用在對數(shù)據(jù)庫的增刪改的敵方.畢竟這是最容易出錯的敵方.為了使程序提高性能,不崩潰的情況下.就是要使用這句語句.

九州編程
TA貢獻1785條經(jīng)驗 獲得超4個贊
你自己拋出的異常啊:
throwstd::overflow_error("The varible rang was more than parameter tha system default");
然后你自己又捕獲了異常.
也就是你捕獲的異常不是
_Varible_Bitset.to_ullong();
拋出的, 這句代碼本身也不會拋出異常
- 2 回答
- 0 關(guān)注
- 126 瀏覽
添加回答
舉報
0/150
提交
取消