|
-
May 17th, 2009, 11:20 AM
#1
Confused by exceptions thrown by iostream classes
Hi gurus,
I'm trying to understand how to properly handle exceptions thrown by the ios classes. "nosuchfile.txt" doesn't exist so I would expect to be thrown an exception indicating just that...
Code:
#include <iostream>
#include <fstream>
using std::cout;
using std::fstream;
int main (int argc, char * const argv[]) {
try
{
fstream ifs;
ifs.exceptions(fstream::failbit);
ifs.open("nosuchfile.txt", fstream::in);
}
catch (fstream::failure& ex)
{
std::cout << ex.what() << std::endl;
}
return 0;
}
Except the output I get is:
basic_ios::clear
Which doesn't seem very helpful. Could someone help me understand what's going on here?
Thanks!
-L
-
May 17th, 2009, 04:50 PM
#2
Re: Confused by exceptions thrown by iostream classes
There is not much information provided by the fstream exception, other than an error occurred when attempting to open or read from the file. Generally speaking, there really is no difference between opening and reading a file. If you can open the file, then you will be able to read it; if you cannot open the file (for whatever reason), then obviously this implies that you cannot read from it either.
The best thing to do is not attempt to interpret (and much less display) the fstream::failure what() statement, but merely to be aware that if it is thrown, the file cannot be opened/read.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|