c++ - Visual Studio 调试持续未处理的异常

标签 c++ visual-studio debugging

我在 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, '\0');
    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;
}

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

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

为什么会发生这种情况?

最佳答案

The result is, that code directly after the exception is executed wich means that f.tellg() returns -1 wich is no valid length for std::string.

调试器为您提供选项,可以像异常不存在一样运行(继续),也可以在异常站点处中断(中断)。

通常,当您选择继续时,应用程序应跳转到异常的 catch block (或将 unwindingm 和 then 堆栈到 catch block )。不过,您没有 catch block ,因此应用程序应该跳转到 std::terminate (并结束执行)。

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

关于c++ - Visual Studio 调试持续未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29668135/

相关文章:

c++ - vector 增长时如何强制执行 move 语义?

c++ - avx512 中比较内在指令的不同语义?

vb.net - Visual Studio 不断更改 resx 文件

visual-studio - Visual Studio 2008 宏仅适用于宏 IDE,不适用于宏资源管理器

unit-testing - 查找错误的 'Saff Squeeze'方法到底是什么?

c++ - 为什么 clang 并不总是为相同的静态 constexpr 产生恒定值

c++ - 自定义类的C++数组,没有匹配的函数调用

c# - 如何告知 NuGet 过去的迁移

javascript - JSHint "not defined"错误

java - 可怕的 : intellij no executable code found at line error