C++ IOS Library - exceptions


Advertisements

Description

It is used to get/set exceptions mask. 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 −

value

(member constants)

indicates functions to check state flags
good() eof() fail() bad() rdstate()
goodbitNo errors (zero value iostate) true false false false goodbit
eofbitEnd-of-File reached on input operation false true false false eofbit
failbitLogical error on i/o operation false false true false failbit
badbitRead/writing error on i/o operation false false true true badbit

Declaration

Following is the declaration for ios::exceptions function.

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

The above first first form (1) returns the current exception mask for the stream.

The above 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).

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

It returns a bitmask of member type iostate representing the existing exception mask before the call to this member function.

Exceptions

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

Data races

Accesses (1) or modifies (2) the stream object.

Concurrent access to the same stream object may cause data races.

Example

In below example explains about ios::fill function.

#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.htm
Advertisements