Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-02-2013, 11:48 AM   #1
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
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:

int readLines(istream &in,
	      int (*hand)(string))
  in.exceptions(istream::failbit | istream::badbit);
      string uname;
      in >> uname;
  return 0;
It is supposed to read in lines, and pass the lines to a handler function. I want it to keep reading lines as long as the input stream is in a good state, return 0 if it reaches eof, and throw an exception for any other input stream error.

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?

terminate called after throwing an instance of 'std::ios_base::failure'
  what():  basic_ios::clear
Old 02-03-2013, 07:37 AM   #2
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.

int readLines(istream &in, int (*hand)(string))

        while (in.good())
    catch (ios_base::failure& e)
        if (!in.eof())
            throw ios_base::failure(e.what());

    return 0;


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
What happens to c++ throw exception? fantasy1215 Programming 3 01-17-2013 09:08 AM
pthread_cancel throw exception but not always sandeep_talan Programming 1 04-22-2010 03:44 PM
C++: thread throw C++ exception and not return from kernel (or SEGV) dk3 Programming 2 11-13-2008 09:15 AM
Using istream::get() & istream::getline() mutiple times kamransoomro84 Programming 0 06-14-2004 07:11 PM
c++ istream issues pH* Programming 3 03-07-2004 10:39 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration