gpt4 book ai didi

c++ - 如何从 boost::filesystem::filesystem_error 获取错误代码的含义

转载 作者:行者123 更新时间:2023-11-30 05:20:50 25 4
gpt4 key购买 nike

我正在考虑从调用 Windows 切换到使用 boost::filesystem。但是,文档几乎没有告诉我如何从中获取有意义的错误信息。

举个简单的例子,我做了以下操作

try
{
// Creates all directories in the path if they do not exist
boost::filesystem::create_directories("!?#Gibberish!?#");
}
catch(boost::filesystem::filesystem_error & e)
{
// Not very clear on how to get meaningful information from the exception
// The codes are found in boost::system::errc::<your code here>
// Try and get the value and then find the Windows codes mapped to the boost codes?
// The actual numeric value can be found in the header with the Windows codes - errno.h under _CRT_NO_POSIX_ERROR_CODES?
//
// You'll have to compare against specific ones and make your own meaningful error message?
const boost::system::error_code errorCode = e.code();

std::ostringstream msg;
msg << "boost::filesystem::create_directories failed with error code: " << errorCode.message();


// Use our own exception type
throw Common::Exception(__FILE__, __LINE__, msg.str());
}

e.code() 在调试器中给我一个值 123。如果我在 Windows header 中查找 123,它会将我指向 ENOPROTOOPT 的 native 错误和 no_protocol_option 的 boost 错误。这是不对的。

该消息有点用处,它说“文件名、目录名或卷标语法不正确”但是,我不确定我是否应该依赖于始终填写或有意义的消息。对于这种情况可能会好很多,switch 语句 + 手动消息似乎很合适。

从 boost::filesystem 中获取有意义的错误信息的正确方法是什么?有意义的是可以查找和比较的字符串消息和错误代码。

编辑:我还发现一些较旧的论坛主题和文章提到了 native_error(),但是,在我的调试器中,版本 1.62 中的异常似乎没有公开任何此类方法。

我找到的相关链接: http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/reference.html#Error-reporting

catching exception from boost::filesystem::is_directory

最佳答案

WinError.h 是这样说的:

//
// MessageId: ERROR_INVALID_NAME
//
// MessageText:
//
// The filename, directory name, or volume label syntax is incorrect.
//
#define ERROR_INVALID_NAME 123L // dderror

使用 errorCode.message(); 作为引用,您总能得到人类可读的错误描述。

关于c++ - 如何从 boost::filesystem::filesystem_error 获取错误代码的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40500301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com