2016-11-09 75 views
1

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

对于一个简单的例子,我做了以下

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值。 如果我在窗口标题中查找123,它会指向ENOPROTOOPT的本地错误和no_protocol_option的升级错误。这是不对的。

该消息有点用处,并说“文件名,目录名称或卷标语法不正确”但是,我不确定我应该依赖于始终填写还是有意义的消息。对于这种情况可能会更好,并且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

+0

的可能的复制[升压错误代码人类可读的描述(http://stackoverflow.com/questions/10755084/boost-error-codes-human-readable-description) – Arunmu

回答

1

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();为quotet,而你总是让人类可读的错误描述。

+0

嗯,所以我们认为他们都关联到https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx并始终有一个字符串?我仍然想制作自己的消息,并在开关或其他东西中比较值。即使是这个例子中的那个也不是最好的。如果是这样的话,那将起作用。我会做一些测试。谢谢。 –

+0

99.9%的系统错误代码具有相关的字符串值。此外 - 在翻译的Windows界面上它已经翻译了消息。 – Alexander