config file loading... exit on error, or warn?

Started by
3 comments, last by MaulingMonkey 19 years, 1 month ago
in my game I check if the xml document exists and if it exists, I load it. If it doesn't I either warn or error and exit, depending on whats being loaded. This is getting quite tedious (always checking if file exists), so Im thinking, what are your thoughts on just putting an error and exit function in the Xml::Document constructor? Good or bad idea?
Advertisement
In the constructor, throw an exception if the file is not found.

In the calling code, either catch the exception and warn, or 'error' by letting the exception fall through and terminate the app.

To avoid cloning the catch code several times, you can wrap it in its own function. Have some sane fall-back thing (an empty XMLDocument instance, whatever 'empty' means) to return in the failure case.
I would do the same: wrap the checking code in a simple stand-alone function or two:

void ExitIfNotFound( filename );
void WarnIfNotFound( filename );
the point is, every time I use it, I have to add the same error message over and over
if(!FileExist(src))        cerr << "error : " << src << " xml file non existant"                << "... unable to continue\n" << Exit(-1);

whereas in some cases, I don't exit. But i'm thinking whether it is wise to exit all the time when there is no config file (2 lines in Xml::Document constructor) OR check the document nearly all the time and warn/exit.

I'd probably do something like leiavoia said (with bool in constructor).

Provide multiple functions as leiavoia suggests. ExitIfNotFound is not meant as a replacement for FileExists, rather, as a supplement.

Personally, I'd suggest the use of exceptions to indicate missing files that are needed for continuation. That way, the client code can choose to either exit, or continue, depending on how it handles the exception.

This topic is closed to new replies.

Advertisement