By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

exception handling for raw read

P: n/a
Does iostream::read() throw any exception already, or do I have to
check with iostream::fail()? Like this:
fstream IN("myfile", ios::in | ios::binary);
char buf[100];

IN.read(buf, 100);
if(IN.fail()) throw "bad input";

Is there any other way to catch bad input? I'm guessing not.

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Amadeus W.M." <am*******@sbcglobal.net> wrote in message
news:pa****************************@sbcglobal.net. ..
Does iostream::read() throw any exception already, or do I have to
check with iostream::fail()? Like this:
fstream IN("myfile", ios::in | ios::binary);
char buf[100];

IN.read(buf, 100);
if(IN.fail()) throw "bad input";

Is there any other way to catch bad input? I'm guessing not.


You guess wrong

Use ios_base::exceptions to tell your IN object to throw an exception on an
error.

john
Jul 22 '05 #2

P: n/a
On Tue, 01 Jun 2004 09:21:12 +0100, John Harrison wrote:

"Amadeus W.M." <am*******@sbcglobal.net> wrote in message
news:pa****************************@sbcglobal.net. ..
[quoted text muted]


You guess wrong

Use ios_base::exceptions to tell your IN object to throw an exception on an
error.


Thanks!
How exactly do I do that? I'm away from home and I don't have
Stroustrup's book with me.

Jul 22 '05 #3

P: n/a
On Tue, 01 Jun 2004 09:21:12 +0100, John Harrison wrote:

"Amadeus W.M." <am*******@sbcglobal.net> wrote in message
news:pa****************************@sbcglobal.net. ..
[quoted text muted]


You guess wrong

Use ios_base::exceptions to tell your IN object to throw an exception on an
error.

john


Ok, I did ios_base::exceptions(ios_base::failbit) and
failure is triggered when a failure occurs, allright.
This is very good, because I'm doing many reads, and I can catch
all and any one of them at the highest level, without having to
check if each one succeeded individually. I guess that's the
purpose of exceptions.

Now, is there any way to make it say where the exception occured?
Something like (__FILE__, __FUNCTION__,__LINE__). Do I derive
my own exception class BadInput : public failure {}; ?
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.