<stdexcept> 头文件中定义的标准异常类

来源:互联网 发布:淘宝宝贝命名规则 编辑:程序博客网 时间:2024/06/06 09:38

exception
The most general kind of problem.
最常见的问题。
runtime_error
Problem that can be detected only at run time.
运行时错误:仅在运行时才能检测到问题
range_error
Run-time error: result generated outside the range of
values that are meaningful.
运行时错误:生成的结果超出了有意义的值域范围
overflow_error
Run-time error: computation that overflowed.
运行时错误:计算上溢
underflow_error
Run-time error: computation that underflowed.
运行时错误:计算下溢
logic_error
Problem that could be detected before run time.
逻辑错误:可在运行前检测到问题
domain_error
Logic error: argument for which no result exists.
逻辑错误:参数的结果值不存在
invalid_argument
Logic error: inappropriate argument.
逻辑错误:不合适的参数
length_error
Logic error: attempt to create an object larger than the
maximum size for that type.
逻辑错误:试图生成一个超出该类型最大长度的对象
out_of_range
Logic error: used a value outside the valid range.

逻辑错误:使用一个超出有效范围的值



下面自己写的测试代码:

注释部门没跑过,所以注释掉了,原因不知道为什么没过。

int _tmain(int argc, _TCHAR* argv[])

try
{
string str( "Micro" );
string rstr( "soft" );
str.append( rstr, 5, 3 );
cout << str.c_str() << endl;
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl << endl;


}


//逻辑错误:length_error
// try
// {
// vector<int, stingyallocator< int > > myv;
// for ( int i = 0; i < 11; i++ )
// myv.push_back( i );
// }
// catch ( exception &e )
// {
// cerr << "Caught " << e.what( ) << endl;
// cerr << "Type " << typeid( e ).name( ) << endl << endl;
// };



//逻辑错误:invalid_argument
try
{
bitset< 32 > bitset( string( "11001010101100001b100101010110000") );
}
catch ( exception &e )
{
cerr << "Caught " << e.what( ) << endl;
cerr << "Type " << typeid( e ).name( ) << endl << endl;
};


try
{
throw domain_error( "Your domain is in error!" );
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid(e).name( ) << endl << endl;
};


//运行时错误:range_error
try
{
throw range_error( "The range is in error!" );
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl << endl << endl;
};


//运行时错误:underflow_error
try
{
throw underflow_error( "The number's a bit small, captain!" );
}
catch ( exception &e ) {
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl << endl;
};


//运行时错误:overflow_error
try
{
bitset< 33 > bitset;
bitset[32] = 1;
bitset[0] = 1;
unsigned long x = bitset.to_ulong( );
}
catch(exception &e)
{
cerr << "Caught " << e.what() << endl;
cerr << "Type: " << typeid(e).name() << endl << endl;
}

system("pause");

return 0;

}

0 0
原创粉丝点击