c++标准库笔记:13.4.4 Stream的状态和异常

设置触发异常

Stream缺省的情况下不抛出异常,但可由成员函数exceptions来设置这种行为。

exceptions有两种形式:

  • 获取引发异常的标志(不带参数)
  • 设置引发异常的标志(带参数)
// This method is not used in msvcm80.dll, so we do not need to compile it.
// Also, if we never change the exception mask, we ensure the clear() method
// will never throw any exception. See VSW#476338 for details.
iostate __CLR_OR_THIS_CALL exceptions() const
{   // return exception mask
    return (_Except);
}

void __CLR_OR_THIS_CALL exceptions(iostate _Newexcept)
{   // set exception mask to argument
    _Except = (iostate)((int)_Newexcept & (int)_Statmask);
    clear(_Mystate);
}

比如strm.exceptions( std::ios::eofbit )表示当Stream被设定了eofbit状态,就触发异常,异常类型为std::ios_base::failure

注:在调用exceptions设置触发异常的标志位,如果这个异常标志位已经在调用之前被设置了,此时就会立刻抛出异常。以上代码已说明一切,exceptions内部会调用clear(_Mystate)来对原来的状态位重新设置下,从而触发异常。

使用异常的注意事项

Stream在读取数据至end-of-file时,不仅设置ios::eofbit,而且还会设置ios::failbit。所以,当发生异常时,要注意使用成员函数eof()来区分到底是哪类异常。

try
{
    cin.clear();
    cin.exceptions( std::ios::eofbit | std::ios::failbit );
    int a = 0;
    while( cin >> a )
    {
        cout << a << endl;
    }
}
catch( const std::ios_base::failure& e )
{
    cout << "ios_base::failure occurred: " << e.what() << endl;
    if ( cin.eof() )
    {
        cout << "end-of-file exception" << endl;
    }
}
catch( ... )
{
    cout << "unexpected exception" << endl;
}

以上例子演示了如何区分异常类型(在windows可通过Ctrl-Z来产生end-of-file,在linux/unix下通过Ctrl-D)。

检测错误+异常

我们可以结合错误检测与异常的方式,来使用自定义错误信息。通过Stream的成员函数eof()fail()bad()来检测出错误,然后抛出自己的错误消息。

try
{
    int a = 0;
    while( cin >> a )
    {
        cout << a << endl;
    }

    if ( cin.eof() )
    {
        throw std::ios::failure("custom error information: "
            "end-of-file have been occurred");
    }
}
catch( const std::ios_base::failure& e )
{
    cout << e.what() << endl;

}
catch( ... )
{
    cout << "unexpected exception" << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值