stl:fstream exeption-handling and file-closing [modified]
-
Dear developers, I have the following code:
try {
ifstream file;
file.open("file.txt");file >> ... ; file.close();
}
catch(......) {
...
---> do I have to close the file here? <----
}My question: Do I have to close the file in the catch-handler? Or is it automatically closed when the destructor of file runs. Kind regards PS: Another question: How can I insert code here with indentation?
modified on Tuesday, May 6, 2008 7:06 AM
-
Dear developers, I have the following code:
try {
ifstream file;
file.open("file.txt");file >> ... ; file.close();
}
catch(......) {
...
---> do I have to close the file here? <----
}My question: Do I have to close the file in the catch-handler? Or is it automatically closed when the destructor of file runs. Kind regards PS: Another question: How can I insert code here with indentation?
modified on Tuesday, May 6, 2008 7:06 AM
Tomerland wrote:
ry { ifstream file; file.open("file.txt"); file >> ... ; file.close(); } catch(...) { ... ---> do I have to close the file here? <---- }
You won't possibly know the exception that took you into the catch block, since you are catching every possible exception. What if the file wasn't opened and the exception is a
FileNotFound
one? Dear God, No!Tomerland wrote:
Another question: How can I insert code here with indentation?
Enclose the code within the <pre> </pre> tags.
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
Dear developers, I have the following code:
try {
ifstream file;
file.open("file.txt");file >> ... ; file.close();
}
catch(......) {
...
---> do I have to close the file here? <----
}My question: Do I have to close the file in the catch-handler? Or is it automatically closed when the destructor of file runs. Kind regards PS: Another question: How can I insert code here with indentation?
modified on Tuesday, May 6, 2008 7:06 AM
Tomerland wrote:
Or is it automatically closed when the destructor of file runs.
Why don't you try? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Dear developers, I have the following code:
try {
ifstream file;
file.open("file.txt");file >> ... ; file.close();
}
catch(......) {
...
---> do I have to close the file here? <----
}My question: Do I have to close the file in the catch-handler? Or is it automatically closed when the destructor of file runs. Kind regards PS: Another question: How can I insert code here with indentation?
modified on Tuesday, May 6, 2008 7:06 AM
Tomerland wrote:
Do I have to close the file in the catch-handler?
if the file was successfully opened, yes, otherwise, no !
Tomerland wrote:
Or is it automatically closed when the destructor of file runs
nop. and as long as you don't know where the exception were thrown, you can't bother about that...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Dear developers, I have the following code:
try {
ifstream file;
file.open("file.txt");file >> ... ; file.close();
}
catch(......) {
...
---> do I have to close the file here? <----
}My question: Do I have to close the file in the catch-handler? Or is it automatically closed when the destructor of file runs. Kind regards PS: Another question: How can I insert code here with indentation?
modified on Tuesday, May 6, 2008 7:06 AM