Quote:Original post by Zipster
As the user of the file class, I would also expect the file to be opened or created.
Well, I'd say it depends on how the class is used.
If I put up a GUI with an "open file" dialog such that it was impossible to provide a non-existent file, failing to open that file would be exceptional.
Quote:If perhaps this was a critical resource like the model or texture package then I might throw an exception if the file couldn't be opened.
What difference does it make if the resource is critical or not?
Quote:
But as far as the file class itself is concerned, this sort of thing happens all the time.
If you mean that it's certainly a case that should be handled, I completely agree.
Quote:It isn't outside the realm of what the file class can reasonably deal with, which is what I meant by "exceptional". It's isn't an uhoh-something-went-wrong-and-my-world-is-collapsing-around-me situation :)
Again I agree, but I don't think an exception implies that "uhoh" situation. I guess your mindset is just different.
Quote:Plus the file class isn't necessarily useless at that point. The object itself should remain in a perfectly valid state so that you can do something like this:FileClass file("this_file_doesn't_exist.txt");if(!file.is_open()) file.open("this_file_does_exist.txt");
You could do the same with a try/catch block. I don't get what you mean here.
But I think we have different ideas about the purpose of the class. I guess you're thinking of it as something which is responsible for looking at both a file's data and also querying its file-system attributes, where as I'm looking at it as something that's merely for reading the data.
If I wanted something to do both, I'd probably do something like:
fs_entry somefile("somefile.txt"); // never throwscout << somefile.exists() << endl;data_stream &ds = somefile.open(); // may throw
All this does beg the question "why not use std::ifstream?", though.
Regardless of the merits of each approach, I still don't get why throwing an exception isn't an *option* for the O.P.