c++ - 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(...)
{
}
如果外部应用程序以外部崩溃错误结束,我无法获得异常。示例:“目录已停止工作”
解决方案
这不是“例外”;这是一个崩溃。
你不能赶上崩溃。
继续的方法是在你的调试器下运行你的程序。然后它将为您提供查找、诊断和纠正问题所需的信息。
推荐阅读
- sql - 在单个查询中汇总和分组多个列值
- c# - 使用在 Service Fabric 上运行的 ASP.NET WebApi 的 Application Insight 进行请求跟踪
- c# - 通过 PowerShell 使用 msbuild 指定 DotNet Framework 4.7 项目的程序集版本
- excel - 如何将数据输入 Internet Explorer 网站?
- reactjs - 如果认证成功反应如何从登录页面重定向到主页
- scala - scala:任何用于将一个双精度除以另一个双精度并将商和余数作为元组返回的内置函数
- c# - Linq 加入同一实体
- python - 如何从 python 脚本中运行 Orange.canvas 并将 Table 转换为它?
- javascript - 是否可以测试一个不返回 chai 的 js 函数?
- php - 如何在流明中格式化json数组对象?