473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

When operator>> can throw...

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. 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.

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.

Does anyone have a better idea, or do I give Foo and error state and
query it after the read?
Jan 19 '08 #1
3 1672
On 2008-01-19 16:15, 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. 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.

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.

Does anyone have a better idea, or do I give Foo and error state and
query it after the read?
Something like this perhaps?

int main() {
ifstream file( "file.txt" );
Foo foo;
int i = 0;
while ( file.eof() == false ) {
try {
file >foo;
} catch ( exception& e )
cout << "error at: " << i << '\n';
continue;
}
++i;
}
}

--
Erik Wikström
Jan 19 '08 #2
Erik Wikström <Er***********@telia.comwrote:
On 2008-01-19 16:15, 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. 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.

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.

Does anyone have a better idea, or do I give Foo and error state and
query it after the read?

Something like this perhaps?

int main() {
ifstream file( "file.txt" );
Foo foo;
int i = 0;
while ( file.eof() == false ) {
try {
file >foo;
} catch ( exception& e )
cout << "error at: " << i << '\n';
continue;
}
++i;
}
}
That doesn't handle the end of file correctly...

I could do something like:

typedef int Foo;

int main() {
ifstream file( "file.txt" );
Foo foo;
int i = 0;
while ( file.eof() == false ) {
try {
while ( file >foo ) {
// process foo
++i;
}
} catch ( exception& e ) {
cout << "error at: " << i << '\n';
}
}
}

But that seems strange.

Does anyone have a different idea?
Jan 19 '08 #3
In article <da****************************@earthlink.vsrv-
sjc.supernews.net>, da******@earthlink.net says...
Erik Wikström <Er***********@telia.comwrote:
On 2008-01-19 16:15, 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. 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.

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.

Does anyone have a better idea, or do I give Foo and error state and
query it after the read?
Something like this perhaps?

int main() {
ifstream file( "file.txt" );
Foo foo;
int i = 0;
while ( file.eof() == false ) {
try {
file >foo;
} catch ( exception& e )
cout << "error at: " << i << '\n';
continue;
}
++i;
}
}
That doesn't handle the end of file correctly...

I could do something like:

typedef int Foo;

int main() {
ifstream file( "file.txt" );
Foo foo;
int i = 0;
while ( file.eof() == false ) {
try {
while ( file >foo ) {
// process foo
++i;
}
} catch ( exception& e ) {
cout << "error at: " << i << '\n';
}
}
}

But that seems strange.

Does anyone have a different idea?
I don't really _like_ it, but this seems at least a bit less convoluted:

int i=1;
for(;;) {
try {
in>>foo;
if (!in)
break;
// process foo
++i;
}
catch(exception const &) {
std::cout << "error at: " << i << "\n";
}
}

I think the better solution would be for a failure to read an object
correctly to set the stream's fail bit to indicate the problem.
Processing this cleanly still isn't entirely trivial, but it's quite a
bit cleaner:

int i = 1;
do {
in.clear();
while (in>>f) {
foos.push_back(f);
++i;
}
if (!in.eof())
std::cout << "Error at: " << i << "\n";
} while (!in.eof());

If you want to produce robust code, something along this general line
(i.e. checking for what went wrong when a stream is no longer good() and
reacting accordingly) is often necessary anyway. Of course, "reacting
accordingly" normally involves more than a completely cryptic error
message, but we'll leave that alone for now...

I think you could make a fair argument that errors in input are to be
expected sufficiently often that throwing an exception from operator>
is never really justified.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 20 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Yu Lianqing | last post by:
Hi, all I am writing an overloading operator >> function for a template class and can't make it right. G++ 3.2 (Redhat8.0) gives the following errors: g++ -c list.cxx In file included from...
7
by: John M | last post by:
Can someone tell how to fix my visual c++ so it can discard this error message. I get this error message error C2593: 'operator >>' is ambiguous But I do not when I am on my mandrake linux...
3
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
4
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
2
by: Bill | last post by:
I am trying to convert a Java APP to C# and it appears C# does not have a >>> operator. Does anyone the best way to conver this operator in to c#? Thanks. -- Bill
7
by: Peter Jansson | last post by:
Dear group, I have been struggling to get a simple program for inserting and extracting std::tm objects to/from streams to work. The code below tries to read a std::tm object from a...
5
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
11
by: Noah Roberts | last post by:
template < typename T > std::istream & operator >(std::istream & in, std::pair<T,T& p) { in >p.first >p.second; return in; } .... std::istream_iterator< std::pair<size_type, size_type
6
by: puzzlecracker | last post by:
Say we have this structure: Struct Foo{ .... friend ostream& operator << (ostream& s, Foo & m); ..... }; friend ostream& operator << (ostream& s, Foo & m){
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.