Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catching exception from boost::filesystem::is_directory

I am currently catching errors from boost::filesystem::is_directory and showing the error to the user by calling "what()" on the exception. This gives the reason for failure but the error is strange to the user. For example:

boost::filesystem::is_directory: Access is denied

How can I catch the boost error and figure out what the actual cause is, so I can show a nicer error message?

like image 211
Warpin Avatar asked Jul 14 '11 17:07

Warpin


1 Answers

By "nicer error message" would you mean something like

#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';
    }
}
like image 152
Cubbi Avatar answered Nov 20 '22 22:11

Cubbi