Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > I/O exceptions weirdness

Reply
Thread Tools

I/O exceptions weirdness

 
 
Amadeus W.M.
Guest
Posts: n/a
 
      09-05-2004
I'm trying to read a list, and watch against failures with try/catch.
The code below reads a list of numbers from stdin. I input the numbers in
several ways:

1) a.out # then enter 1 2 3 4 5 Ctrl-D manually.
2) echo "1 2 3 4 5" | a.out
3) cat listfile | a.out
4) a.out < listfile

In each case an exception is thrown.

Is this normal behavior? Can anyone elaborate? It happens with
gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) and
gcc version 3.4.0 (Red Hat Linux 3.4.0-1)


Thanks!


#include <iostream>
#include <cstdlib>
#include <list>

using namespace std;


void print_state(ios_base::iostate & s)
{
cerr << "g: " << (s & ios_base::goodbit ? 1 : 0) << " "
<< "b: " << (s & ios_base::badbit ? 1 : 0) << " "
<< "f: " << (s & ios_base::failbit ? 1 : 0) << " "
<< "e: " << (s & ios_base::eofbit ? 1 : 0) << endl;
}


int main(int argc, char * argv[])
{
double xtmp;
list<double> x;

try{

print_state(cin.exceptions());
cin.exceptions(ios_base::badbit | ios_base::failbit);

while(cin>>xtmp) x.push_back(xtmp);

}
catch(ios_base::failure & err){
print_state(cin.exceptions());
}

return 0;
}

 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      09-05-2004
Amadeus W.M. wrote in news(E-Mail Removed)
in comp.lang.c++:

> I'm trying to read a list, and watch against failures with try/catch.
> The code below reads a list of numbers from stdin. I input the numbers
> in several ways:
>
> 1) a.out # then enter 1 2 3 4 5 Ctrl-D manually.
> 2) echo "1 2 3 4 5" | a.out
> 3) cat listfile | a.out
> 4) a.out < listfile
>
> In each case an exception is thrown.


You get what you ask for.

>
> Is this normal behavior? Can anyone elaborate? It happens with


Yep.

> gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) and
> gcc version 3.4.0 (Red Hat Linux 3.4.0-1)


> cin.exceptions(ios_base::badbit | ios_base::failbit);


The above tells cin to raise an exception when either badbit or
failbit is set.

>
> while(cin>>xtmp) x.push_back(xtmp);


The while will never fail as when cin>>xtmp fail's an exception is
raised (due to failbit being set) and caught below.

>
> }
> catch(ios_base::failure & err){
> print_state(cin.exceptions());
> }
>


HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exceptions - How do you make it work like built-in exceptions? Lie Python 3 01-14-2008 06:45 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
Tkinter WEIRDNESS or Python WEIRDNESS? steve Python 4 03-13-2005 12:34 AM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments