Processing exception between loading and displaying data

I had a situation that was not sure how best to deal with this. Entrance will be appreciated. Imagine I have a method like this:

void loaddata()
{
    try
    {
        // EXTRA: I also want to skip below SaveSomething if there was exeption
        // last time I called DecryptAndloadXMLdata. This may happen
        // if user calls loaddata twice. This is exaclty similar situation
        // as app quitting just it happens is user calls loaddata twice
        // and during first call there was exception say with DecryptAndloadXMLdata
        Savesomething(listOfObjects, xmlPath);//save old data first
        xmlPath = newValue;

        // some operations

        xmlDoc = DecryptAndloadXMLdata(xmlPath);

        // some other operations- populate List with data from above XML file
        listOfObjects.Add(objectFromXML);
        // Here also possibly modify contents of listOfObjects elements
    }
    catch(Exception ex)
    {
        xlmPath="";
    }
}

Now the thing is, when the application leaves, I have such a function for automatically saving the List object, the method filled in above, to a file. How:

void whenAppisQuitting()
{
    Savesomething(listOfObjects, xmlPath);
}

But that is the problem. Imagine xmlDoc = loadXMLdata();throwing the method above. What happens, the list I mentioned will not be filled, and when the application leaves empty elements (for example, empty listOfObjects), it will be written to xmlPath- thus, damage to my source file, because there was an unrelated exception due to encryption in loadXMLData.

, . ? , , , xmlPath catch - , - , - , , xmlPath ="". ?

, - ?

+4
2

?

, . string.Empty (IMO). , , .

catch(Exception ex)
{
    // Log
    IsParsingSuccessful = false;
}

, :

void AppIsQuitting()
{
    if (IsParsingSuccessful)
    {
        SaveSomething(listOfObjects, xmlPath);
    }
}
+1

, , .

, /boolean . true , true, .

0

Source: https://habr.com/ru/post/1589515/


All Articles