Exception

S

sam

Hi,

I m using BSD stl with g++ compiler.

The following code dos not throw exception if the input file is not exist:

try {
fin.open(f, ios::in);
}
catch (std::exception& e)
{
cout << "Exception: " << e.what();
}

How can I correct this problem?

I have the following includes in the .hh file:
#include <iostream>
#include <string>
#include <fstream>
#include <exception>


Thanks
Sam.
 
V

Victor Bazarov

sam said:
I m using BSD stl with g++ compiler.

The following code dos not throw exception if the input file is not exist:

try {
fin.open(f, ios::in);
}
catch (std::exception& e)
{
cout << "Exception: " << e.what();
}

How can I correct this problem?

I have the following includes in the .hh file:
#include <iostream>
#include <string>
#include <fstream>
#include <exception>

Who says it should throw?

V
 
P

Peter MacMillan

sam said:
Hi,

I m using BSD stl with g++ compiler.

The following code dos not throw exception if the input file is not exist:

try {
fin.open(f, ios::in);
}
catch (std::exception& e)
{
cout << "Exception: " << e.what();
}

How can I correct this problem?

You need to enable the exception. see:
http://www.cplusplus.com/ref/iostream/ios/exceptions.html

example:
//---
#include <iostream>
#include <fstream>
#include <exception>

int main() {

using std::fstream;

try {
fstream fin;
fin.exceptions( fstream::failbit );
fin.open("nosuchfile.ext", fstream::in);
}
catch (std::exception &e) {
std::cout << "Exception: " << e.what();
}
}
//---


outputs:
Exception: ios_base::failbit set

Thanks
Sam.


--
Peter MacMillan
e-mail/msn: (e-mail address removed)
icq: 1-874-927

GCS/IT/L d-(-)>-pu s():(-) a- C+++(++++)>$ UL>$ P++ L+ E-(-) W++(+++)>$
N o w++>$ O !M- V PS PE Y+ t++ 5 X R* tv- b++(+) DI D+(++)>$ G e++ h r--
y(--)
 
D

Dirk Wendland

sam said:
Hi,

I m using BSD stl with g++ compiler.

The following code dos not throw exception if the input file is not exist:

try {
fin.open(f, ios::in);
}
catch (std::exception& e)
{
cout << "Exception: " << e.what();
}

How can I correct this problem?

I have the following includes in the .hh file:
#include <iostream>
#include <string>
#include <fstream>
#include <exception>


Thanks
Sam.
Hello

fin.open(f, ios::in); thorows no execeptions

g++ Standard :

__filebuf_type*
open(const char* __s, ios_base::eek:penmode __mode);

Greetings
Dirk
 
S

sam

Thanks for the hints.
It caught the exception of "file not found" when added the following
line before the "try" block:

fin.exceptions ( ifstream::eofbit | ifstream::failbit | ifstream::badbit );

Sam.
 
J

Jonathan Arnold

sam said:
Thanks for the hints.
It caught the exception of "file not found" when added the following
line before the "try" block:

fin.exceptions ( ifstream::eofbit | ifstream::failbit | ifstream::badbit );

You don't need all of these, right? Just the failbit one. eofbit means
it reached the end of file, which isn't normally a throwable offense:)
 
S

sam

Jonathan said:
You don't need all of these, right? Just the failbit one. eofbit means
it reached the end of file, which isn't normally a throwable offense:)
arh... yes, thanks for reminding me. :)

Sam.
 
K

Karl Heinz Buchegger

sam said:
Hi,

I m using BSD stl with g++ compiler.

The following code dos not throw exception if the input file is not exist:

try {
fin.open(f, ios::in);
}
catch (std::exception& e)
{
cout << "Exception: " << e.what();
}

fin.open(f, ios::in);
if( !fin ) {
// something went wrong in the open
}
 
S

sam

This try block always catch exception even if the input file is opened
successfully. I think I should give up using exception handling with
this simple case. Exception handling might be better used in complex
situiations.

fin.open(f, ios::in);
if( !fin ) {
// something went wrong in the open
}
This works fine. But I thought exception handling is a generic solution
to deal with error, which is more elegant.

Sam
 
D

Donovan Rebbechi

This try block always catch exception even if the input file is opened
successfully.

Not sure what you mean. if fin is an istream, open() does not throw any
exceptions (even if it fails), so the try is unnecessary.
I think I should give up using exception handling with
this simple case. Exception handling might be better used in complex
situiations.

No, exception handling might be better used, when there is an exception to
handle.

If the open() method threw an exception when it failed (e.g. file doesn't
exist) then a try/catch block would be appropriate. But using an exception
to handle a failure for a file to open is OK if you've got some class that
opens files.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,127
Latest member
CyberDefense
Top