473,789 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1705
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.vsr v-
sjc.supernews.n et>, da******@earthl ink.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
2444
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 SLinkList.cxx:3, from list.cxx:1: SLinkList.h:20: ISO C++ forbids declaration of `type name' with no
7
9190
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 platform. Why is this, a bug in visual c++ ? John J
3
2052
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 fine? // --------- foo.cpp --------- #include <iostream> using namespace std;
4
2483
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 templatized operator>>() as a member that can deal with the built in C types (int, char, float...) template <class tType> STR& STR::operator>>(tType & t); I then attempted to add an overloaded version of this to support my own
2
12908
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
3519
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 std::istringstream but fails to do so, could anybody see what is wrong with the code? (Output follows the code.) I fear that I have not completely grasped how the time_getXXX methods should be used in the operator<< ? With best regards,
5
3635
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
11
2062
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
1935
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
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10412
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10142
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9021
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.