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


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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值