cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : IOstream Library : ios : exceptions
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
IOstream Library
manipulators
classes:
· filebuf
· fstream
· ifstream
· ios
· iostream
· ios_base
· istream
· istringstream
· ofstream
· ostream
· ostringstream
· streambuf
· stringbuf
· stringstream
objects:
· cerr
· cin
· clog
· cout
types:
· fpos
· streamoff
· streampos
· streamsize
ios
· ios::ios
· ios::~ios
member functions:
· ios::bad
· ios::clear
· ios::copyfmt
· ios::eof
· ios::exceptions
· ios::fail
· ios::fill
· ios::good
· ios::imbue
· ios::init
· ios::narrow
· ios::operator!
· ios::operator void*
· ios::rdbuf
· ios::rdstate
· ios::setstate
· ios::tie
· ios::widen

-

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::iostream, 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

// 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> )
iostate exceptions () const;
iostate exceptions ( iostate except );

See also

ios::rdstate Get error state flags (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us