Visual Studio 调试继续未处理的异常
Visual Studio debug continuing unhandled exception
我在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::终止"是疯狂的(因为这样,"继续"实际上意味着"停止")。因此,在这种情况下,"继续"意味着"继续执行,就好像异常不存在一样",而不是"继续执行以终止应用程序"。
相关文章:
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 在C++程序中使用的迭代器中未处理的异常
- 在 ******.exe 中0x00000000772CA267 (ntdll.dll) 处未处理的异常:0xC0000
- 未处理的异常与 cudaMemcpy2D
- 处理未处理的异常更改C++
- for 循环中未处理的异常
- VisualC++ 2010 有没有办法找出有关未处理异常错误的更多详细信息
- 为什么此代码导致未处理的异常失败
- 在OpenCV_core310.dll中使用findContours函数时,OpenCV引发未处理的异常
- 0x0F50DF58:0xC0000005:访问冲突读取位置0x0047CA04时未处理的异常
- 使用 getline 时未处理的异常
- 错误 : 异常 : 引发未处理的异常: 读取访问冲突.0xDDDDDDDD临时温度
- 核心消息传递中未处理的异常.dll在程序关闭期间
- 在 Hello World.exe 中0x0F828F0E (ucrtbased.dll) 处未处理的异常: 0xC00
- 从向量中删除元素时未处理的异常
- C++ 程序中双链表的未处理异常
- 在 Myfile.exe 中0x00831D39时未处理的异常:0xC00000FD:堆栈溢出(参数:0x0000000
- CUDA 编程未处理的异常和堆栈溢出
- 在多线程c++ 11程序中,当异常未处理时会发生什么