Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c++;?_C++_Linux_Exception - Fatal编程技术网

C++ 如何在c++;?

C++ 如何在c++;?,c++,linux,exception,C++,Linux,Exception,我正在尝试使用 system("mkdir -p a/b/c/d") 在C++中,在linux中创建目录。我目前对C++的异常处理过程没有太多的了解。使用try/catch抛出异常的正确方法是什么?如果命令执行失败,我应该抛出什么异常?更易于使用,更安全,而且定义良好(或在C++17中)。请使用此解决方案 #include <boost/filesystem.hpp> int main() { namespace fs = boost::filesystem; f

我正在尝试使用

system("mkdir -p a/b/c/d")

在C++中,在linux中创建目录。我目前对C++的异常处理过程没有太多的了解。使用try/catch抛出异常的正确方法是什么?如果命令执行失败,我应该抛出什么异常?

更易于使用,更安全,而且定义良好(或在C++17中)。请使用此解决方案

#include <boost/filesystem.hpp>

int main()
{
    namespace fs = boost::filesystem;
    fs::create_directories("/tmp/path/to/dir");
    fs::create_directories("/dev/null");
}
你可以用。或者(更好)system()不会抛出任何内容。如果你想在失败的时候扔东西,扔什么取决于你自己。
#include <cstdlib>
#include <stdexcept>

int main()
{
    if ( std::system("mkdir -p /tmp/path/to/dir") != 0 )
        throw std::runtime_error("Could not create directory");

    if ( std::system("mkdir -p /dev/null") != 0 )
        throw std::runtime_error("Could not create directory");
}