C++ 在运行无效命令c++;

C++ 在运行无效命令c++;,c++,exception,boost,command-line,C++,Exception,Boost,Command Line,我正在使用boost从我的应用程序运行命令行命令。我使用的是封装到helper函数中的以下代码: tuple<string, int> Utility::RunCommand(const string& arguments) const { string response; int exitCode; try { ipstream iStream; auto childProcess = child(argume

我正在使用boost从我的应用程序运行命令行命令。我使用的是封装到helper函数中的以下代码:

tuple<string, int> Utility::RunCommand(const string& arguments) const
{
    string response;
    int exitCode;
    try
    {
        ipstream iStream;
        auto childProcess = child(arguments, std_out > iStream);
        string line;

        while (getline(iStream, line) && !line.empty())
        {
            response += line;
        }

        childProcess.wait();
        exitCode = childProcess.exit_code();
    }
    catch (...)
    {
        // log error
        throw;
    }

    return make_tuple(response, exitCode);
}

如何使其运行以返回非零错误代码而不是引发异常?

这就是您的
catch
子句处理异常的目的。它不必重新播放它们:

tuple<string, int> Utility::RunCommand(const string& arguments) const
{
    string response;
    int exitCode;
    try
    {
        ipstream iStream;
        auto childProcess = child(arguments, std_out > iStream);
        string line;

        while (getline(iStream, line) && !line.empty())
        {
            response += line;
        }

        childProcess.wait();
        exitCode = childProcess.exit_code();
    }
    catch (PlatformNotSupportedException& e)
    {
        std::cerr << "That operation is not supported on this platform." << std::endl;
        exit(1);
    }
    catch (...)
    {
        std::cerr << "Unspecified error occurred." << std::endl;
        exit(1); // give nonzero exit code
        //throw; // take out this
    }

    return make_tuple(response, exitCode);
}
tuple实用程序::RunCommand(常量字符串和参数)常量
{
字符串响应;
int exitCode;
尝试
{
同流峡流;
自动childProcess=child(参数,标准输出>iStream);
弦线;
while(getline(iStream,line)&&!line.empty())
{
响应+=行;
}
childProcess.wait();
exitCode=childProcess.exit_code();
}
捕获(平台不支持异常和e)
{

std::cerr问题是您的可执行文件不存在。这就是Powershell所抱怨的。您是否编译代码以获得
伪cmd.exe
?@doron True,我知道这一部分。我想知道是否有办法调用该命令,使其返回错误代码,而不是抛出异常?@FredLarson No.dummy-cmd.exe在一些机器上存在,而在其他机器上则不存在。我可以这样做。但是有没有一种方法可以避免陷入捕获?基本上是一种执行命令的方法,这样它就可以从方法返回。还有,exit(1)退出应用程序?我想返回调用者。@commandocaddy,请稍候,您想在该方法中抛出并捕获异常,然后返回吗?还是根本不想出现异常?理想情况下,根本不存在异常。因为大量计算机将不具有此可执行文件。我想防止异常处理ng,因为它可能很昂贵。@commandocaddy那么为什么不修改您的方法以不引发异常,而只是发出相应的退出代码呢?这样您就不需要异常处理了。
tuple<string, int> Utility::RunCommand(const string& arguments) const
{
    string response;
    int exitCode;
    try
    {
        ipstream iStream;
        auto childProcess = child(arguments, std_out > iStream);
        string line;

        while (getline(iStream, line) && !line.empty())
        {
            response += line;
        }

        childProcess.wait();
        exitCode = childProcess.exit_code();
    }
    catch (PlatformNotSupportedException& e)
    {
        std::cerr << "That operation is not supported on this platform." << std::endl;
        exit(1);
    }
    catch (...)
    {
        std::cerr << "Unspecified error occurred." << std::endl;
        exit(1); // give nonzero exit code
        //throw; // take out this
    }

    return make_tuple(response, exitCode);
}