2011-07-14 41 views
7

我目前正在從boost :: filesystem :: is_directory捕獲錯誤,並通過調用異常的「what()」向用戶顯示錯誤。這給出了失敗的原因,但錯誤對用戶來說很奇怪。例如:從boost :: filesystem :: is_directory捕獲異常

的boost ::文件系統:: is_directory:訪問被拒絕

我如何能趕上增壓誤差,找出真正的原因是什麼,所以我可以告訴一個更好的錯誤信息?

+1

Boost.Filesystem的例外中的[Boost.System]術語來實現(http://www.boost.org/doc/libs/ release/libs/system/doc/index.html) - 你讀過它的文檔嗎? – ildjarn

回答

15

通過 「更好的錯誤消息」 您將意味着像

#include <iostream> 
#include <boost/filesystem.hpp> 
int main() 
{ 
    boost::filesystem::path p("/proc/1/fd/1"); 
    try { 
     boost::filesystem::is_directory(p); 
    } catch(const boost::filesystem::filesystem_error& e) 
    { 
     if(e.code() == boost::system::errc::permission_denied) 
      std::cout << "Search permission is denied for one of the directories " 
        << "in the path prefix of " << p << "\n"; 
     else 
      std::cout << "is_directory(" << p << ") failed with " 
        << e.code().message() << '\n'; 
    } 
} 
+0

是的,這確實是一個非常好的錯誤信息。 – Warpin

+2

注意:您可能需要捕獲boost :: filesystem :: wfilesystem_error是您指定的路徑使用wchar_t * – Warpin