使用 Qt Creator 编译时未捕获我的异常
My exception is not caught when compiling with Qt Creator
我已经编写了一个简单的基于控制台的网络服务器,我正在尝试将其移植到 Qt,以便我可以为其开发 GUI。
在我控制从硬盘读取文件的 class 中,我一直在使用异常来指示读取文件时出现错误等
现在,当我尝试 运行 使用 Qt 5.7 编译的代码时,我的 catch 块突然没有接收到我的异常。相反,它把它一直扔回去并崩溃。
但是当我编写 catch(...) 来获取所有类型的异常时它可以正常工作而不会崩溃..
这是我的文件读取函数中的代码:
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() returns "basic_ios::clear" 和 typeid(e).name( ) returns "NSt8ios_base7failureE"。
我在 QtCreator 中使用 MinGw 5.3.0 32 位进行编译,我在异常工作时使用的编译器是 MinGw-w64 4.7.3。
问题似乎是 bug gcc >= 5.0 附带的 libstdc++:标准库的某些部分使用 C++98 ABI 抛出异常,而捕获代码使用 C++11 ABI .该错误仍未修复,除了降级编译器和标准库或不将异常与 iostream 一起使用外,似乎没有简单的解决方法。
我已经编写了一个简单的基于控制台的网络服务器,我正在尝试将其移植到 Qt,以便我可以为其开发 GUI。 在我控制从硬盘读取文件的 class 中,我一直在使用异常来指示读取文件时出现错误等
现在,当我尝试 运行 使用 Qt 5.7 编译的代码时,我的 catch 块突然没有接收到我的异常。相反,它把它一直扔回去并崩溃。 但是当我编写 catch(...) 来获取所有类型的异常时它可以正常工作而不会崩溃..
这是我的文件读取函数中的代码:
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() returns "basic_ios::clear" 和 typeid(e).name( ) returns "NSt8ios_base7failureE"。
我在 QtCreator 中使用 MinGw 5.3.0 32 位进行编译,我在异常工作时使用的编译器是 MinGw-w64 4.7.3。
问题似乎是 bug gcc >= 5.0 附带的 libstdc++:标准库的某些部分使用 C++98 ABI 抛出异常,而捕获代码使用 C++11 ABI .该错误仍未修复,除了降级编译器和标准库或不将异常与 iostream 一起使用外,似乎没有简单的解决方法。