LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-02-2013, 11:48 AM   #1
hydraMax
Member
 
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:

Code:
int readLines(istream &in,
	      int (*hand)(string))
{
  in.exceptions(istream::failbit | istream::badbit);
  while(in.good())
    {
      string uname;
      in >> uname;
      hand(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?

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

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.

Code:
int readLines(istream &in, int (*hand)(string))
{
    ...

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

    return 0;
}
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:43 PM.

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