linux - how to throw exception with system() in c++? -


i trying use

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

in c++ create directory in linux. not have understanding of c++'s exception handling process. proper way use try/catch throw exception, , exception should throw, in case command execution fails?

much easier use, safer, , well-defined boost.filesystem (or <experimental/filesystem> in c++17). please use solution.

#include <boost/filesystem.hpp>  int main() {     namespace fs = boost::filesystem;     fs::create_directories("/tmp/path/to/dir");     fs::create_directories("/dev/null"); } 

the return value of system implementation defined, expected status code returned called command. if command returns else 0 failed.

#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"); } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -