首页 > 解决方案 > C++ Windows:处理应用程序外部崩溃进入程序

问题描述

我有以下功能来执行外部程序:

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;
}

是否可以通过这种方式将崩溃作为异常处理(我以 dir 为例,它不会崩溃)?

std::ostringstream ossCmd;
ossCmd << "dir";
std::string cmd = ossCmd.str();

try
{
   std::string str = exec(cmd.c_str());
}
catch(...)
{

}

如果外部应用程序以外部崩溃错误结束,我无法获得异常。示例:“目录已停止工作”

标签: c++c++11visual-c++c++14

解决方案


这不是“例外”;这是一个崩溃。

你不能赶上崩溃。

继续的方法是在你的调试器下运行你的程序。然后它将为您提供查找、诊断和纠正问题所需的信息。


推荐阅读