C++ std :: filesystem :: copy抛出怪异的例外:commonutil :: chresultexceptionimpl

发布于 2025-01-24 09:04:05 字数 2704 浏览 0 评论 0原文

这是一个特定于Windows的问题。

我正在尝试使用std :: Filesystem :: copy()将目录的内容复制到另一个目录。

当调试到我的C ++程序时,我注意到执行std :: filesystem :: copy()之后发生了一个奇怪的例外。只有在调试而无法捕获时才能看到此例外。

我用其他目录对其进行了测试,但没有发生例外。

这是例外的方式:


我从称为test_fail构造的目录中复制内容:

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

复制的内容将进入一个空名,称为test 。

每个文件的大小为1 kb (每个文件中只有字符串“ hello”)


这是我的代码:

#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
}

发生例外的屏幕截图:

“在此处输入图像描述”

您可以看到异常仅出于某种原因在调试时发生。

尽管我的代码在例外效果很好,但是为什么会发生这种情况?

这是Visual Studio中的错误吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文