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

Exception

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
sam wrote:
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
Jul 23 '05 #2

P: n/a
sam wrote:
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/iostrea...xceptions.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: pe***@writeopen.com
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(--)
Jul 23 '05 #3

P: n/a
sam wrote:
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::openmode __mode);

Greetings
Dirk
--
develop - Projekt
A Tool for c-c++ / java / html / php
http://dirk-wendland.de.vu
Jul 23 '05 #4

P: n/a
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.
Jul 23 '05 #5

P: n/a
sam wrote:
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:-)

--
Jonathan Arnold (mailto:jd******@buddydog.org)
The Incredible Brightness of Seeing, a Home Theater weblog
http://www.anaze.us/HomeTheater
Jul 23 '05 #6

P: n/a
sam
Jonathan Arnold wrote:
sam wrote:
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:-)

arh... yes, thanks for reminding me. :)

Sam.
Jul 23 '05 #7

P: n/a
sam wrote:

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
}

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #8

P: n/a
sam
Karl Heinz Buchegger wrote:
sam wrote:
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();
} 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
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.


Jul 23 '05 #9

P: n/a
On 2005-04-08, sam <sa*****@authtec.com> wrote:
try {
fin.open(f, ios::in);
}
catch (std::exception& e)
{
cout << "Exception: " << e.what();
}
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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.