我的异常在编译Qt Creator时没有被捕获

My exception is not caught when compiling with Qt Creator

本文关键字:Creator 异常 编译 Qt 我的      更新时间:2023-10-16

我写了一个简单的基于控制台的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一起使用之外,似乎没有简单的解决方案。