jeudi 2 juillet 2015

Error handling when parsing text file to object

I want to parse a simple text file and create an object from the data it contains. I'm using C++11 for this (and I'm not fluent).

In case of any kind of error (e.g. missing file or invalid text) I wish to tell the caller of my parsing function what went wrong, providing information like what kind of error occurred and where in the file.

I don't consider exceptional that errors occur while parsing, so it seems exceptions are not the way to go.

I thought of returning a struct with all the info, including the resulting parsed object in case of success:

struct ParsingResult
{
    bool success;
    int errorCode;
    int errorLine;
    ParsedObject object;
}

However I'm not convinced by this solution because, in case of errors, I must still provide a ParsedObject. I can define a default constructor for that, of course, but by it's nature a ParsedObject makes sense only when the parsing is successful.

I could change ParsedObject to ParsedObject*, but I'm reluctant to use pointers when not necessary, and I wonder if this can be avoided.

My question: can you suggest a better solution to this problem? What is it?

Aucun commentaire:

Enregistrer un commentaire