ios::exceptions


public member function
iostate exceptions ( ) const;
void exceptions ( iostate except );

Get/set exception mask

The first function version returns the current exception mask for the stream.

The second function version sets a new exception mask and calls clear(rdstate()).

The exception mask is an internal value of all stream objects specifying which state flags have to throw an exception when they are set. This mask is an object of type ios_base::iostate, which is a value formed by any combination of the following member constants:

flag valueindicates
eofbitEnd-Of-File reached while performing an extracting operation on an input stream.
failbitThe last input operation failed because of an error related to the internal logic of the operation itself.
badbitError due to the failure of an input/output operation on the stream buffer.
goodbitNo error. Represents the absence of all the above (the value zero).


More than a single state flag bit can be combined into a single bitmask by using the bitwise OR operator (|). By default, stream objects have a goodbit exception mask, which means no exceptions are thrown when any of the state flags is set.

Parameters

except
A bitmask value of type ios_base::iostate formed by a combination of error state flag bits to be set (badbit, eofbit, failbit).


Return Value

A bitmask of type ios_base::iostate representing the existing exception mask before the call to this member function.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// ios::exceptions
#include <iostream>
#include <fstream>
using namespace std;

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

  file.close();

  return 0;
}


Basic template member declaration

( basic_ios<charT,traits> )
1
2
iostate exceptions () const;
iostate exceptions ( iostate except );


See also