Benjamin Helfman
asked on
Meaning of "exceptions()" in "ist.exceptions( ist.exceptions() | ios_base:: badbit);"
I am reading Stroustrup's book: Programming Principles and Practice Using C++ and came across this code which I do not understand:
ist.exceptions( ist.exceptions() | ios_base:: badbit);
He says that "the effect is simply that from that statement onward, ist will throw the standard library exception ios_base:: failure if it goes bad()."
I understand that the vertical pipe is a bitwise OR operator. I think that this code sets the badbit. I don't understand the effect of the exceptions() on the left side of the OR operator.
ist.exceptions( ist.exceptions() | ios_base:: badbit);
He says that "the effect is simply that from that statement onward, ist will throw the standard library exception ios_base:: failure if it goes bad()."
I understand that the vertical pipe is a bitwise OR operator. I think that this code sets the badbit. I don't understand the effect of the exceptions() on the left side of the OR operator.
ASKER
So the effect of the inner exceptions call is to leave existing bits set the way they already were? And the effect of leaving out the inner call would be to set all other bits to false and bad it to true?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the std::ios::exceptions is a function which gets/sets an exception mask in the file which is used by the file object to decide in which situations it should throw an exception or not.
There exist two implementations of this function:
- iostate exceptions() const; // get current bit mask
- void exceptions (iostate except); // set new bit mask
So the statement you posted set's the bit ios_base::badbit using binary OR to combine it with the mask already set in the file object which is returned by the inner exception() call.
Hope this helps,
ZOPPO