1、bitset类提供to_ulong操作,如果bitset提供的位数大于unsigned long的长度,则抛出一个overflow_error异常。书写一个程序能捕获这种异常并输出提示信息。
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<100> bs;
for (size_t ix=0;ix!=bs.size();ix++)
{
bs[ix]=1;
}
try
{
bs.to_ulong();
}catch(runtime_error err){cout<<err.what()<<endl;}
return 0;
}输出为:bitset<N> overflow
2、runtime_error类:The class serves as the base class for all exceptions thrown to report errors presumably detectable only when the program executes.
#include <iostream>
using namespace std;
int main( )
{
// runtime_error
try
{
locale loc( "test" );
}
catch (exception &e)
{
cerr << "Caught " << e.what( ) << endl;
cerr << "Type " << typeid( e ).name( ) << endl;
};
}输出:Caught bad locale name Type class std::runtime_error