我的异常在编译Qt Creator时没有被捕获
My exception is not caught when compiling with Qt Creator
我写了一个简单的基于控制台的web服务器,我试图移植到Qt,这样我就可以为它开发一个GUI。在控制从硬盘读取文件的类中,我一直使用异常来指示何时读取文件等出现错误。
现在当我尝试运行用Qt 5.7编译的代码时,我的catch块不会突然捡起我的异常。相反,它会把它扔回去,然后崩溃。但是当我写catch(…)来拾取所有类型的异常时,它可以工作而不会崩溃…
下面是我的filereading函数中的代码: fstream file;
file.exceptions( ifstream::failbit | ifstream::badbit );
try{
string content;
//Open file and read the file to string
file.open(this->getDirectory() + filename, ios_base::in|ios_base::binary);
file.seekg(0, file.beg);
char tmpChar = 0;
while( file.peek() != EOF )
{
file.read(&tmpChar, sizeof(tmpChar));
content.push_back(tmpChar);
}
file.close();
unique_ptr<fileObject> tmpPtr( new fileObject(filename, content, "text/html") );
if( this->addFileToCache(move(tmpPtr) ))
return true;
return false;
}
catch(const ios_base::failure& e){
if(file.is_open()) file.close();
return false;
}
为什么Qt不能工作?catch(…)捡起ios_base::failure异常,所以我不明白为什么我的代码不再工作了。
更新:当我用catch(exception &e)捡起异常并打印其信息时,我得到以下结果:. .what()返回"basic_ios::clear",typeid(e).name()返回"NSt8ios_base7failureE"。
我在QtCreator中编译MinGw 5.3.0 32位,当我的异常工作时我使用的编译器是MinGw-w64 4.7.3。
问题似乎是gcc>= 5.0附带的libstdc++的一个错误:标准库的某些部分使用c++ 98 ABI抛出异常,而捕获代码使用c++ 11 ABI。这个错误仍然没有修复,除了降级编译器和标准库或不使用异常与iostreams一起使用之外,似乎没有简单的解决方案。
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 无法使用Qt Creator在Windows中构建yaml-cpp
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 如何在QT Creator上将QWidget声明为继承类的对象
- 尝试使用智能指针时引发异常
- 我的异常在编译Qt Creator时没有被捕获