Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Error with ifstream and exceptions (http://www.velocityreviews.com/forums/t743167-error-with-ifstream-and-exceptions.html)

Marco 02-07-2011 12:59 AM

Error with ifstream and exceptions
 
Hi,

in the following code I get an error message after the file is output. What
did I do wrong?

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main() {
ifstream fileIn;
fileIn.exceptions ( ifstream::failbit | ifstream::badbit );
try {
fileIn.open( "file" ); }
catch ( ifstream::failure e ) {
cout << "Error."; }

string line;
while ( getline( fileIn, line ) ) {
cout << line << endl;
}
fileIn.close();
return 0;
}

The error message after the output of »file« is:

terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_ios::clear
Aborted


Regards
Marco


Jonathan Lee 02-07-2011 01:58 AM

Re: Error with ifstream and exceptions
 
On Feb 6, 6:59*pm, Marco <net...@lavabit.com> wrote:
> in the following code I get an error message after the file is output. What
> did I do wrong?


The "while" line is the culprit:

> * while ( getline( fileIn, line ) ) {
> * * cout << line << endl;
> * }


specifically, it continues to execute until the result of getfile(),
i.e.,
fileIn, evaluates to false. This happens via ios::operator void*(),
which
will be NULL (false for the purposes of the while) exactly when
failbit
or badbit is set. But you told it to throw an exception when these
bits
are set, so the while loop doesn't terminate properly. The exception
gets
thrown first.

BTW, the whole operator void*() thing is explained here:
http://www.parashift.com/c++-faq-lit....html#faq-15.4

--Jonathan

Marco 02-07-2011 02:28 AM

Re: Error with ifstream and exceptions
 
On 2011-02-06 Jonathan Lee <jonathan.lee.975@gmail.com> wrote:

> The "while" line is the culprit:
>
> > * while ( getline( fileIn, line ) ) {
> > * * cout << line << endl;
> > * }

>
> specifically, it continues to execute until the result of getfile(),
> i.e.,
> fileIn, evaluates to false. This happens via ios::operator void*(),
> which
> will be NULL (false for the purposes of the while) exactly when
> failbit
> or badbit is set.


This is the behaviour I expected.

> But you told it to throw an exception when these
> bits
> are set, so the while loop doesn't terminate properly. The exception
> gets
> thrown first.


Why does it throw an exception? The while block is outside of the try block.
Sorry, maybe this is a stupid question, I'm a beginner in C++. But I expected
exceptions only to be thrown in the try block. What else is it for?

> BTW, the whole operator void*() thing is explained here:
> http://www.parashift.com/c++-faq-lit....html#faq-15.4


Thanks for the link.


Regards
Marco


Ian Collins 02-07-2011 03:57 AM

Re: Error with ifstream and exceptions
 
On 02/ 7/11 03:28 PM, Marco wrote:
> On 2011-02-06 Jonathan Lee<jonathan.lee.975@gmail.com> wrote:
>
>> The "while" line is the culprit:
>>
>>> while ( getline( fileIn, line ) ) {
>>> cout<< line<< endl;
>>> }

>>
>> specifically, it continues to execute until the result of getfile(),
>> i.e.,
>> fileIn, evaluates to false. This happens via ios::operator void*(),
>> which
>> will be NULL (false for the purposes of the while) exactly when
>> failbit
>> or badbit is set.

>
> This is the behaviour I expected.
>
>> But you told it to throw an exception when these
>> bits
>> are set, so the while loop doesn't terminate properly. The exception
>> gets
>> thrown first.

>
> Why does it throw an exception?


You told it to.

> The while block is outside of the try block.


That isn't relevant, exceptions can be thrown anywhere.

> Sorry, maybe this is a stupid question, I'm a beginner in C++. But I expected
> exceptions only to be thrown in the try block. What else is it for?


To catch exceptions thrown within it.

--
Ian Collins

Paul 02-07-2011 11:02 AM

Re: Error with ifstream and exceptions
 

"Ian Collins" <ian-news@hotmail.com> wrote in message
news:8r98tfFv2tU3@mid.individual.net...
> On 02/ 7/11 03:28 PM, Marco wrote:
>> On 2011-02-06 Jonathan Lee<jonathan.lee.975@gmail.com> wrote:
>>
>>> The "while" line is the culprit:
>>>
>>>> while ( getline( fileIn, line ) ) {
>>>> cout<< line<< endl;
>>>> }
>>>
>>> specifically, it continues to execute until the result of getfile(),
>>> i.e.,
>>> fileIn, evaluates to false. This happens via ios::operator void*(),
>>> which
>>> will be NULL (false for the purposes of the while) exactly when
>>> failbit
>>> or badbit is set.

>>
>> This is the behaviour I expected.
>>
>>> But you told it to throw an exception when these
>>> bits
>>> are set, so the while loop doesn't terminate properly. The exception
>>> gets
>>> thrown first.

>>
>> Why does it throw an exception?

>
> You told it to.
>
>> The while block is outside of the try block.

>
> That isn't relevant, exceptions can be thrown anywhere.
>

Ian Collins obviously doesn't know how to answer the question properly.
I don't know how to answer the question but I know enough to get by.
Ian is different from most of the other arseholes around here because he is
obviously the most clueless of them all.

>> Sorry, maybe this is a stupid question, I'm a beginner in C++. But I
>> expected
>> exceptions only to be thrown in the try block. What else is it for?

>
> To catch exceptions thrown within it.
>


Ian obvioulsy gets confused between the try block and the catch block. :)


Marco 02-07-2011 12:57 PM

Re: Error with ifstream and exceptions
 
On 2011-02-07 "Paul" <pchristor@yahoo.co.uk> wrote:

> >> Why does it throw an exception?

> >
> > You told it to.
> >
> >> The while block is outside of the try block.

> >
> > That isn't relevant, exceptions can be thrown anywhere.
> >

> Ian Collins obviously doesn't know how to answer the question properly.
> I don't know how to answer the question but I know enough to get by.
> Ian is different from most of the other arseholes around here because he is
> obviously the most clueless of them all.
>
> >> Sorry, maybe this is a stupid question, I'm a beginner in C++. But I
> >> expected
> >> exceptions only to be thrown in the try block. What else is it for?

> >
> > To catch exceptions thrown within it.
> >

>
> Ian obvioulsy gets confused between the try block and the catch block. :)


I'm confused, too. Exceptions can be thrown everywhere, is that right? But
what is the purpose of the try block? Why isn't it enough to write a catch
block if the exceptions can be thrown in the try block and outside the try
block? (I know it's not valid to write catch without try, it's just to
understand how things work.)


Regards
Marco


Paul 02-07-2011 01:47 PM

Re: Error with ifstream and exceptions
 

"Marco" <netuse@lavabit.com> wrote in message
news:4d4fec21$0$7660$9b4e6d93@newsspool1.arcor-online.net...
> On 2011-02-07 "Paul" <pchristor@yahoo.co.uk> wrote:
>
>> >> Why does it throw an exception?
>> >
>> > You told it to.
>> >
>> >> The while block is outside of the try block.
>> >
>> > That isn't relevant, exceptions can be thrown anywhere.
>> >

>> Ian Collins obviously doesn't know how to answer the question properly.
>> I don't know how to answer the question but I know enough to get by.
>> Ian is different from most of the other arseholes around here because he
>> is
>> obviously the most clueless of them all.
>>
>> >> Sorry, maybe this is a stupid question, I'm a beginner in C++. But I
>> >> expected
>> >> exceptions only to be thrown in the try block. What else is it for?
>> >
>> > To catch exceptions thrown within it.
>> >

>>
>> Ian obvioulsy gets confused between the try block and the catch block. :)

>
> I'm confused, too. Exceptions can be thrown everywhere, is that right? But
> what is the purpose of the try block? Why isn't it enough to write a catch
> block if the exceptions can be thrown in the try block and outside the try
> block? (I know it's not valid to write catch without try, it's just to
> understand how things work.)
>
>

Yeah Exceptions can be thrown anytime.
A try block is used to enclose a piece of code that you think might possibly
throw an exception, for example opening a file. You can either re-throw the
exception or catch the exception that has been thrown from within a try
block.
Exceptions can be thrown by the OS or from code, there are different types
of exceptions. Your try/catch block is looking for an exception to be thrown
by the OS on file.open but this doesn't happen. A C++ exception is thrown in
the while loop and this is not handled inside a try block.
I think any code that can possibly invoke a C++ throw( C++ Exception) must
be enclosed in a try block, but not 100% sure, try putting the while loop in
a try/catch block and see what happens. :)
..

HTH


Marco 02-07-2011 02:04 PM

Re: Error with ifstream and exceptions
 
On 2011-02-07 "Paul" <pchristor@yahoo.co.uk> wrote:

> Yeah Exceptions can be thrown anytime.
> A try block is used to enclose a piece of code that you think might
> possibly throw an exception, for example opening a file. You can either
> re-throw the exception or catch the exception that has been thrown from
> within a try block.
> Exceptions can be thrown by the OS or from code, there are different types
> of exceptions. Your try/catch block is looking for an exception to be
> thrown by the OS on file.open but this doesn't happen. A C++ exception is
> thrown in the while loop and this is not handled inside a try block.
> I think any code that can possibly invoke a C++ throw( C++ Exception) must
> be enclosed in a try block, but not 100% sure, try putting the while loop
> in a try/catch block and see what happens. :)


Thanks for your explanation. That make things more clear to me. I tried the
following:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main() {
ifstream fileIn;
fileIn.exceptions ( ifstream::failbit | ifstream::badbit );
try {
fileIn.open( "file" ); }
catch ( ifstream::failure e ) {
cout << "Error."; }

string line;
try {
while ( getline( fileIn, line ) ) {
cout << line << endl;
}
}
catch (...) {}

fileIn.close();
return 0;
}

It works. But looking at this code other questions arise. Is this the
preferred way to do it? Can I stop exceptions to be thrown, since I don't know
which code might possibly throw an excetion. I think about something like

fileIn.exceptions ( none );

after the (first) catch block. That way I don't have to put everything that
might throw an exception in a try block, to make my code more readable.


Regards
Marco


Jonathan Lee 02-07-2011 02:34 PM

Re: Error with ifstream and exceptions
 
On Feb 7, 6:57*am, Marco <net...@lavabit.com> wrote:
> I'm confused, too. Exceptions can be thrown everywhere, is that right? But
> what is the purpose of the try block? Why isn't it enough to write a catch
> block if the exceptions can be thrown in the try block and outside the try
> block? (I know it's not valid to write catch without try, it's just to
> understand how things work.)
>
> Regards
> Marco


Hi Marco,
as others have mentioned, an exception can be thrown anywhere. The
purpose
of the try/catch structure is to indicate where you are interested in
responding to exceptions (try), and what you're going to do when one
occurs
(catch).

In the example you posted, for instance, you respond to
ifstream::failure
exceptions by printing a message. All other exceptions are left
unhandled
by your code, and continue with the default behavior (stack
unwinding).

The reason you might do this is that some exceptions may be
"expected".
Or perhaps its better to say some exceptions can be recovered from.
Like reading from a file might suddenly fail if the file were on a
network
and someone kicked out the network cable. When the file can't be read
from,
your program may be able to continue by simply printing a message to
the
user and quitting from operating on the file.

On the other hand, if while reading from that file the OS runs out
of
memory, the program probably can't continue. So you don't catch it;
you
let the exception propagate until it terminates your program.

--Jonathan


Paul 02-07-2011 02:43 PM

Re: Error with ifstream and exceptions
 

"Marco" <netuse@lavabit.com> wrote in message
news:4d4ffbec$0$7661$9b4e6d93@newsspool1.arcor-online.net...
> On 2011-02-07 "Paul" <pchristor@yahoo.co.uk> wrote:
>
>> Yeah Exceptions can be thrown anytime.
>> A try block is used to enclose a piece of code that you think might
>> possibly throw an exception, for example opening a file. You can either
>> re-throw the exception or catch the exception that has been thrown from
>> within a try block.
>> Exceptions can be thrown by the OS or from code, there are different
>> types
>> of exceptions. Your try/catch block is looking for an exception to be
>> thrown by the OS on file.open but this doesn't happen. A C++ exception is
>> thrown in the while loop and this is not handled inside a try block.
>> I think any code that can possibly invoke a C++ throw( C++ Exception)
>> must
>> be enclosed in a try block, but not 100% sure, try putting the while loop
>> in a try/catch block and see what happens. :)

>
> Thanks for your explanation. That make things more clear to me. I tried
> the
> following:
>
> #include <iostream>
> #include <string>
> #include <fstream>
> using namespace std;
>
> int main() {
> ifstream fileIn;
> fileIn.exceptions ( ifstream::failbit | ifstream::badbit );
> try {
> fileIn.open( "file" ); }
> catch ( ifstream::failure e ) {
> cout << "Error."; }
>
> string line;
> try {
> while ( getline( fileIn, line ) ) {
> cout << line << endl;
> }
> }
> catch (...) {}
>
> fileIn.close();
> return 0;
> }
>
> It works. But looking at this code other questions arise. Is this the
> preferred way to do it? Can I stop exceptions to be thrown, since I don't
> know
> which code might possibly throw an excetion. I think about something like
>
> fileIn.exceptions ( none );
>
> after the (first) catch block. That way I don't have to put everything
> that
> might throw an exception in a try block, to make my code more readable.
>
>

It depends what you are doing.
Many code examples only use try block around the file.open part, so they
only want to stop the program from crashing in the event of a file.open
error.
Removing "fileIn.exceptions ( ifstream::failbit | ifstream::badbit );" might
solve your problem.

I would check a few online tutorials and examples , there loads of them
about file streams. Choose the option that suits you best.



All times are GMT. The time now is 09:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.