ok, i have a question

say i have a class that wraps all file accesing functions ( basicly opening, reading and closing )
all these file handling functions throw a FileException exception
i have a try block in that i open, read and close my file using the above mentioned class
let's say read method throws a FileException
that means the close method is not getting called, so in order to close the file i have to close it
in the catch (FileException &blabla) block ...
but what if the close method inside the catch also throws an exception?
so the question is: how do i desing the try catch block so that no FileException can be left uncatched

thx

First of all, tell your compiler.
Then type some code, your blabla is not very good for understanding.

The STL file close functions don't throw exceptions. They will set the fail bit if pending output could not be written before closing, but that's all.

The general rule is that file close functions should not throw exceptions...

The STL file close functions don't throw exceptions. They will set the fail bit if pending output could not be written before closing, but that's all.

The general rule is that file close functions should not throw exceptions...

i'm not talking about the stl file close. when i mean close throws an exception i mean my "homemade" close method of my
homemade" File class ( that actually wraps the stl close in a "object oriented manner") throws that exception.
i would also like to know why would close functions not throw exeptions.
thx

C++ FAQ-Lite (on destructors and exceptions).
MSDN blog (clean-up functions can't fail because...)

Some googled examples of why throwing exceptions in clean-up code is bad:
Karsten Wagner's blog
Don't Commit Errorcide by Paul Kimmel

Admittedly, this is an issue which has people on both sides of the aisle, but in my opinion at least, cleanup should almost never get in the way. Enough people agree with me that there is a programming paradigm that emphasizes this fact: RAII.

No OS that I know of will tell you that you are not allowed to close a file.

Hope this helps.

yes, true. I documented on RAII too, the solution is to close in the File object destructor
thx for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.