473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Exception

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
9 2851
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nicolas Fleury | last post by:
Hi, I've made a small utility to re-raise an exception with the same stack as before with additional information in it. Since I want to keep the same exception type and that some types have very...
1
by: Old Wolf | last post by:
1. What is the difference between #include <stdexcept> and #include <exception> ? 2. Is there a list somewhere of what each standard exception is used for? either to throw them, or throw...
11
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two...
4
by: maricel | last post by:
I have the following base table structure - DDL: CREATE TABLE "ADMINISTRATOR"."T1" ( "C1" INTEGER NOT NULL ) IN "TEST_TS" ; ALTER TABLE "ADMINISTRATOR"."T1" ADD PRIMARY KEY
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
6
by: Vadivel Kumar | last post by:
I've a problem in handling a custom exception The following is my custom exception class: public class AppException : public Exception { public AppException (string message, Exception...
3
by: JohnDeHope3 | last post by:
First let me say that I understand that Asp.Net wraps my exception in an HttpUnhandledException. I have found a lot of discussion about that on the web, which was informative, but not helpful. Let...
7
by: Sek | last post by:
Hi Folks! I was pondering over a code and noticed that exception handlers were present in the private, protected as well as public methods. And, ofcourse, public methods were calling priv/prot...
2
by: Darko Miletic | last post by:
Recently I wrote a dll in c++ and to simplify the distribution I decided to link with multithreaded static library (/MT or /MTd option). In debug everything works fine but in release I get this: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.