locked
Why can StorageFolder.GetFolderAsync(myDir) throw exception instead of returning null if myDir does not exist? RRS feed

  • General discussion

  • What is the advantage of throwing an exception rather than returníng null in the quite possible case that the demanded directory does not exist?

    In my case I was trying to rebuild Directory.CreateDirectory functionality to build a given folder hirachy from one path string as an extension to System.IO.Path.

    So I came up with this:

    public async static Task<StorageFolder> CreateFolderHirachyAsync(this StorageFolder Parent, string FolderHirachy) { StorageFolder newFolder = Parent; // keep track of the so far built FolderHirachy StorageFolder rollbackFolder = null; // in case of error rollback all created Folders try { if (Parent == null || FolderHirachy.IndexOfAny(Path.GetInvalidPathChars()) >= 0) return null; // can't create FolderHirachy without starting point or with invalid characters in it if (Parent.Path.Length > 0) // if FolderHirachy already contains full path of Parent, remove this part from the FolderHirachy FolderHirachy = FolderHirachy.Replace(Parent.Path, ""); String[] subDirs = FolderHirachy.Split(_pDelimiters); // break FolderHirachy up into separate subdirectories foreach (var subDir in subDirs) { if (subDir.Length > 0) // build subdirectories that actually have a Name { // take existing if there is one - otherwise create one and remember the first created for later rollback, if needed newFolder = await newFolder.GetFolderAsync(subDir) ?? (rollbackFolder != null ? await newFolder.CreateFolderAsync(subDir, CreationCollisionOption.OpenIfExists) : rollbackFolder = await newFolder.CreateFolderAsync(subDir, CreationCollisionOption.OpenIfExists));

    if (newFolder == null) throw new Exception("FolderHirachy could not be built!"); } } return newFolder; } catch (Exception e) { newFolder = null; try { if (rollbackFolder != null) rollbackFolder.DeleteAsync(); } catch (Exception _e) { throw new Exception(e.Message, _e); } return null; } }

    But the problem is, that GetFolderAsync does throw an exception instead of just returning NULL if the required folder does not exist. So I had to replace the one code line

    ...

    newFolder = await newFolder.GetFolderAsync(subDir) ?? (rollbackFolder != null ? await newFolder.CreateFolderAsync(subDir, CreationCollisionOption.OpenIfExists) : rollbackFolder = await newFolder.CreateFolderAsync(subDir, CreationCollisionOption.OpenIfExists));

    ...

    with this

                try
                {
                  newFolder = await intermediateFolder.GetFolderAsync(subDir);
                }
                catch
                {
                  newFolder = null;  // this has to be because the next block can't be awaited in a catch block!
                }
                if (newFolder == null)
                {
                  newFolder = await intermediateFolder.CreateFolderAsync(subDir);
                  if (rollbackFolder == null)
                    rollbackFolder = newFolder;
                }
    intermediateFolder = newFolder;

    and had to introduce a new variable intermediateFolder.

    An other solution for my problem would be, if CreateFolderAsync(subDir, CreationCollisionOptions.OpenIfExists) would give me a way to distinguish if the folder was created or just opened, than I could set the rollbackFolder to the first newly created folder by checking this. But still, failing to access a non existing folder/file could always return NULL, rather than throwing an exception.



    • Edited by Scruff.R Thursday, January 10, 2013 1:50 PM one-line code snipped not displayed
    • Changed type Jesse Jiang Friday, January 11, 2013 8:43 AM
    Thursday, January 10, 2013 1:48 PM

All replies

  • Hello,

    Thanks for your feedback.

    I would like to change this thread type as discussion, because I think there may be no exact answer for this question.

    Thanks for your understanding,

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 11, 2013 8:42 AM