
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ IOS Library - exceptions
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() | ||
goodbit | No errors (zero value iostate) | true |
false |
false |
false |
goodbit |
eofbit | End-of-File reached on input operation | false |
true |
false |
false |
eofbit |
failbit | Logical error on i/o operation | false |
false |
true |
false |
failbit |
badbit | Read/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; }