Originally Posted by Daved
Here are two file open scenarios that should be more clear:
Scenario 1
- Function gets filename as parameter from the caller.
- Intent of function is to load additional non-essential initialization preferences for application.
- Function uses fstream to attempt to open the file.
- Loading no preferences is a valid result of calling the function.
So, if the file fails to open, the fstream's is_open() function will return false, and the function can handle this by loading no preferences. The function can return false if it failed to load preferences if the designer chooses to do so. The situation is not exceptional.
Scenario 2
- Function gets filename as parameter from the caller.
- Intent of the function is to read in necessary initialization data from a dat file.
- Function uses fstream to open the file.
- Application cannot run without data from the file.
So, if the file fails to open, the fstream's is_open() function will return false, but the function cannot handle this error. It's job is to load required information from a dat file. If it cannot load that information, it cannot do its job and the app cannot continue. It should throw an exception.