C++ std::filesystem::copy throws weird exception : CommonUtil::CHResultExceptionImpl

Viewed 395

This is a Windows specific problem.

I am trying to use std::filesystem::copy() to copy a directory's contents to another directory.

When debugging into my C++ program , I notice that a weird exception occurs after executing std::filesystem::copy(). This exception can only be seen when debugging and can't be caught.

I tested it with other directories and the exception didn't occur for them.

Here is how The exception occurs :


I copy the contents from a directory called test_fail structured like so :

                                 test_fail
                                     |
               ----------------------------------------------
               |                                            |
          Subdirectory                                 Subdirectory
               |                                            |
         --------------                          -------------------------
               |                                 |                       |
            1 file                            7 files               Subdirectory
                                                                    -------------
                                                                         |
                                                                      3 files

The copied contents will go into an empty directory called test.

Every file has is 1 KB of size (Every file only has the string "hello" in it )


Here is my code :

#include <filesystem>
#include <string>

namespace fs = std::filesystem;


int main() {

    // The path of the Directory that produces the exception :  CommonUtil::CHResultExceptionImpl

    std::string failing_Directory = "C:\\Users\\Ibrahim\\test_fail"; 


    // Copying the failing Directory to specifed directory in a try block ,
    // instead of catching the exception it just copies the contents and the function returns.

    // I'm using options "fs::copy_options::recursive | fs::copy_options::overwrite_existing"
    // to copy all files as well as subdirectories to the specified location as well as overwrite 
    // any overwrite the existing directories and files that are duplicates

    try {
        fs::copy(failing_Directory, "C:\\Users\\Ibrahim\\test", fs::copy_options::recursive | fs::copy_options::overwrite_existing);
    }
    catch (std::exception &e) {

    // The Program doesnt enter this catch

        printf(e.what());
        return -1;
    }

    // The Program Exits Normally
}

Screenshot of where the exception occurs :

enter image description here

You can see the exception occur only while debugging , for some reason .

Although my code works fine with the exception , why is this happening ?

Is this a bug in Visual Studio ?

Thank you

0 Answers
Related