lundi 30 avril 2018

C++ Windows: Catching exception when executing external application into the program

I have the following function to execute a external program:

std::string exec(const char* cmd) {
    char buffer[128];
    std::string result = "";
    FILE* pipe = _popen(cmd, "r");
    if (!pipe) throw std::runtime_error("_popen() failed!");
    try {
        while (!feof(pipe)) {
            if (fgets(buffer, 128, pipe) != NULL)
                result += buffer;
        }
    } catch (...) {
        _pclose(pipe);
        throw;
    }
    _pclose(pipe);
    return result;
}

Is it possible to catch the exception in this way?

try
{
   exec(cmd.c_str());
}
catch(...)
{

}

Aucun commentaire:

Enregistrer un commentaire