简体   繁体   English

异常处理不适用于 Windows 上的 Qt

[英]Exception handling doesn't work with Qt on Windows

I'm facing strange problem.我面临着奇怪的问题。 Namely, Qt somehow turns off exception handling in my program.也就是说,Qt 以某种方式关闭了我程序中的异常处理。 I can't catch any exception, and when I throw an exception application crashes.我无法捕获任何异常,并且当我抛出异常时应用程序崩溃。

I'm using Qt 4.7.0 (32 bit) from Qt SDK v2010.05 on Windows 7 (64 bit), g++ (GCC) 4.5.1 from MinGW, NetBeans 6.9.1.我在 Windows 7(64 位)上使用 Qt SDK v2010.05 的 Qt 4.7.0(32 位),MinGW 的 g++(GCC)4.5.1,NetBeans 6.9.1。 But I also cheked this with g++ 3.4.5 (also from MinGW) and Qt Creator 2.0.1 - same strange behavior.但我也用 g++ 3.4.5(也来自 MinGW)和 Qt Creator 2.0.1 对此进行了检查 - 同样奇怪的行为。

For example (simplest case):例如(最简单的情况):

#include <Qt/QApplication.h>
#include <iostream>
#include <stdexcept>
#include <cstdlib>

using namespace std;


int main(int argc, char* argv[]) {
    QApplication app(argc, argv);

    try {
        cout << "Before exception" << endl;
        throw runtime_error("Exception occured");
        cout << "After exception" << endl;
    } catch (runtime_error& exc) {
        cout << exc.what() << endl;
        exit(1);
    }

    return 0;
}

When I execute above program I've got this output:当我执行上面的程序时,我得到了这个输出:

Before exception异常前

This application has requested the Runtime to terminate it in an unusual way.此应用程序已请求运行时以一种不寻常的方式终止它。
Please contact the application's support team for more information.请联系应用程序的支持团队了解更多信息。

I've tried to add flag "-fexceptions" to g++ but it hasn't changed anything.我试图向 g++ 添加标志“-fexceptions”,但它没有改变任何东西。

When I don't use Qt, everything is OK:当我不使用 Qt 时,一切正常:

#include <Qt/QApplication.h> // It is not caused only by including Qt header
                             // so it doesn't matter if I comment this out or not
#include <iostream>
#include <stdexcept>
#include <cstdlib>

using namespace std;


int main(int argc, char* argv[]) {
    // QApplication app(argc, argv);

    try {
        cout << "Before exception" << endl;
        throw runtime_error("Exception occured");
        cout << "After exception" << endl;
    } catch (runtime_error& exc) {
        cout << exc.what() << endl;
        exit(1);
    }

    return 0;
}

The output:输出:

Before exception异常前
Exception occured发生异常

Does anybody know why is this happen that way and how to fix this?有谁知道为什么会这样以及如何解决这个问题? Has it something to do with type of exception handling method (SJLJ or Dwarf-2) used when Qt was build?是否与构建 Qt 时使用的异常处理方法类型(SJLJ 或 Dwarf-2)有关?

I've reconfigured and recompiled Qt with flag -exceptions :我已经重新配置并重新编译了带有标志-exceptions Qt:
D:\\Qt\\2010.05\\qt>mingw32-make confclean && configure -exceptions && mingw32-make
and now everything is ok!现在一切正常!

Thanks all for help, especially to Nick D!感谢大家的帮助,尤其是 Nick D!

Anyway, it's very strange that I had Qt build without this flag.无论如何,我在没有这个标志的情况下构建 Qt 是很奇怪的。 I had downloaded Qt SDK in binary form from official site.我从官方网站下载了二进制形式的 Qt SDK。

It's no longer necessary to use the -exceptions flag with Qt.不再需要在 Qt 中使用 -exceptions 标志。 In Qt Creator 4 it's the default, and my Windows Qt app happily uses vast and extensive exception handling with no problems.在 Qt Creator 4 中,这是默认设置,我的 Windows Qt 应用程序愉快地使用了大量且广泛的异常处理,没有任何问题。 Qt MSVC builds use the /EHsc compiler option, which turns normal exception handling on. Qt MSVC 构建使用 /EHsc 编译器选项,它打开正常的异常处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM