Visual Studio 调试继续未处理的异常

Visual Studio debug continuing unhandled exception

本文关键字:异常 未处理 继续 Studio 调试 Visual      更新时间:2023-10-16

我在Visual Studio(2008年和2013年)中发现了一种奇怪的行为。

让我们从一个测试代码开始:

#include <string>
#include <fstream>
#include <iostream>
using namespace std;
string readFile()
{
    std::fstream f;
    f.open("not_good_file.txt", std::fstream::in);
    if (!f.good())
        throw std::exception("unable to read file");
    f.seekg(0, f.end);
    std::streamoff len = f.tellg();
    f.seekg(0, f.beg);
    string result(len, '');
    f.read(&result[0], len);
    f.close();
    return result;
}
int main(int argc, char** argv)
{
    cout << "before exception" << endl;
    readFile();
    cout << "after exception" << endl;
    return 0;
}

在没有调试器的情况下运行时,应用程序会按预期中止,但是当我在调试时遇到无人保护的异常时,将显示已知的消息窗口,告诉我有关未捕获的异常的信息。我可以选择break它显示抛出异常的行(如果可用)以及continue选项(顾名思义)继续应用程序。

结果是,该代码在异常执行后立即执行,这意味着f.tellg()返回-1这不是std::string的有效长度。

为什么会这样?

结果是,在异常执行后直接执行代码意味着 f.tellg() 返回 -1 对于 std::string 来说不是有效的长度。

调试器为您提供了运行的选项,就像异常不存在一样(继续)或在异常站点中断(中断)。

通常,当您选择继续时,应用程序应跳转到异常的 catch 块(或堆栈展开,然后跳转到 catch 块)。但是,您没有 catch 块,因此应用程序应跳转到std::terminate(并结束执行)。

让 IDE 为您提供"继续"的选项,这意味着"调用 std::终止"是疯狂的(因为这样,"继续"实际上意味着"停止")。因此,在这种情况下,"继续"意味着"继续执行,就好像异常不存在一样",而不是"继续执行以终止应用程序"。