c++ istream: throw exception for everything but eof
Hi. I'm not especially familiar with c++, but I am required to use it for a class. I wrote this little function:
Code:
int readLines(istream &in, However, when I tested this, it also throws an exception when eof is reached, which is not what I intended. How do I get it to not throw an exception on eof, but to throw an exception for everything else? Code:
terminate called after throwing an instance of 'std::ios_base::failure' |
The problem is that when EOF is reached, the failbit is set, and hence the reason an exception is thrown.
I would suggest that you wrap your while-loop within a try-block, and then catch the ios_base::failure exception when it occurs. Then examine the eofbit to determine if the exception was thrown due to reaching the EOF, or due to some other reason. For the latter, merely re-throw the exception. Code:
int readLines(istream &in, int (*hand)(string)) |
All times are GMT -5. The time now is 01:41 AM. |