ios::exceptions()函数

它用于获取/设置异常掩码。异常掩码是所有流对象保存的内部值,指定为哪些状态标志设置时抛出成员类型失败(或某些派生类型)的异常。

声明

以下是ios::exceptions函数的声明。

get (1)    iostate exceptions() const;
set (2)    void exceptions (iostate except);

上述第一种形式(1)返回流的当前异常掩码。
上面的第二种形式(2)为流设置一个新的异常掩码,并清除流的错误状态标志(好像调用了成员clear())。

参数

  • except − 设置错误状态标志位(badbiteofbit和/或failbit)的组合形成的成员类型iostate的位掩码值,或者设置为goodbit(或零)。

返回值

  • 在调用此成员函数之前返回表示现有异常掩码的成员类型iostate的位掩码。

示例

下面的例子演示了ios::fill函数。

#include <iostream>     
#include <fstream>      

int main () {
  std::ifstream file;
  file.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
  try {
    file.open ("test.txt");
    while (!file.eof()) file.get();
    file.close();
  }
  catch (std::ifstream::failure e) {
    std::cerr << "Exception opening/reading/closing file/n";
  }

  return 0;
}

上一篇: ios::fill()函数 下一篇: ios::imbue()函数