public member function
<ios> <iostream>

std::ios::exceptions

get (1)
iostate exceptions() const;
set (2)
void exceptions (iostate except);
Get/set exceptions mask
The first form (1) returns the current exception mask for the stream.

The second form (2) sets a new exception mask for the stream and clears the stream's error state flags (as if member clear() was called).

The exception mask is an internal value kept by all stream objects specifying for which state flags an exception of member type failure (or some derived type) is thrown when set. This mask is an object of member type iostate, which is a value formed by any combination of the following member constants:

iostate value
(member constants)
indicatesfunctions to check state flags
good()eof()fail()bad()rdstate()
goodbitNo errors (zero value iostate)truefalsefalsefalsegoodbit
eofbitEnd-of-File reached on input operationfalsetruefalsefalseeofbit
failbitLogical error on i/o operationfalsefalsetruefalsefailbit
badbitRead/writing error on i/o operationfalsefalsetruetruebadbit
eofbit, failbit and badbit are member constants with implementation-defined values that can be combined (as if with the bitwise OR operator), so that the stream throws when any of the selected error state flags is set.
goodbit is zero, indicating that no exceptions shall be thrown when an error state flags is set.

All streams have goodbit by default (they do not throw exceptions due to error state flags being set).

Parameters

except
A bitmask value of member type iostate formed by a combination of error state flag bits to be set (badbit, eofbit and/or failbit), or set to goodbit (or zero).

Return Value

The first form (1) returns a bitmask of member type 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
// ios::exceptions
#include <iostream>     // std::cerr
#include <fstream>      // std::ifstream

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;
}

Data races

Accesses (1) or modifies (2) the stream object.
Concurrent access to the same stream object may cause data races.

Exception safety

Basic guarantee: if an exception is thrown, the stream is in a valid state.

See also