从boost :: filesystem :: is_directory捕获异常 [英] catching exception from boost::filesystem::is_directory
本文介绍了从boost :: filesystem :: is_directory捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在捕获来自boost :: filesystem :: is_directory的错误,并通过在异常中调用what()向用户显示错误。这给出了失败的原因,但用户的错误是奇怪的。例如:
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:访问被拒绝
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?
推荐答案
通过更好的错误消息,您的意思是像
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';
}
}
这篇关于从boost :: filesystem :: is_directory捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文