Re: When operator>> can throw...
Daniel T. wrote:
This is basically what I have now...
class Foo { /* definition irrelevant */ };
istream& operator>>( istream& is, Foo& foo ); // could throw
int main() {
ifstream file( "file.txt" );
Foo foo;
int i = 0;
try {
while ( file >> foo ) {
//process foo;
++i;
}
}
catch ( exception& e ) {
cout << "error at: " << i << '\n';
}
}
The problem I'm having is that op>> could throw.
The question is: what can it throw, and do you want to handle
it? A well written >> will probably only throw something like
bad_alloc, which you generally cannot really handle anyway.
If it does, I don't want to process the foo object that threw,
but I want the program to report an error and continue
extracting foo objects.
Doesn't that depend on why it threw?
I could make op>> a no-throw and give Foo some sort of error
or invalid state, but that doesn't feel right. I can't report
the error inside the op>> because I don't have enough
information to make a full report.
I'm not sure I understand. Error reporting for istream's is
pretty much standardized: if you encounter a format error, or
there is nothing left to read, you set failbit, a hard read
error, badbit, and while not really an error, anytime you see
EOF, you should set eofbit.
After that, it is the users responsibility to determine whether
he wants that error to throw or no. It's sometimes (often?)
appropriate in the case of badbit, only rarely for failbit, and
probably never for eofbit.
Note that recovering from the error can be more or less
difficult: you have to clear the error in the stream, of course,
but you also have to resynchronize. (For line oriented input,
where each record is a line, it is usual to use getline(), and
then use an istringstream to parse the text. This automatically
leaves the stream "synchronized" for the next input.)
Does anyone have a better idea, or do I give Foo and error
state and query it after the read?
I doubt that Foo should have error state, but istream already
has it.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34