Exception handling doesn't work with Qt on Windows
Asked Answered
R

2

10

I'm facing strange problem. Namely, Qt somehow turns off exception handling in my program. 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. But I also cheked this with g++ 3.4.5 (also from MinGW) and Qt Creator 2.0.1 - same strange behavior.

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.

When I don't use Qt, everything is OK:

#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?

Returnable answered 10/11, 2010 at 21:12 Comment(6)
Whew, you scared me! We're probably about to switch to Qt on my recommendation and this would have been a killer. Good thing it works with VS.Torrez
Where does it crash exactly? Any chance you can find a more exact location? QApplication's notify() perhaps?Enroll
probably it's configured with the -no-exceptions flag. Try to reconfigure and re-make the Qt SDK.Gildus
Hmm, this is verbatim a Microsoft CRT message. Does mingw emulate it that well? Doubtful and probably the reason for this problem.Branchia
@Hans Passant: Mingw links against MSVCRT.Hermosa
Encountered the same problem, but for now no solution works for me. Although I believe revers pointed out to the right solution, I cannot make it work, yet. I've recompiled the Qt twice with no success by now. Which version of MinGW and compilers do you use?Daves
R
8

I've reconfigured and recompiled Qt with flag -exceptions:
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!

Anyway, it's very strange that I had Qt build without this flag. I had downloaded Qt SDK in binary form from official site.

Returnable answered 11/11, 2010 at 2:27 Comment(0)
I
0

It's no longer necessary to use the -exceptions flag with Qt. In Qt Creator 4 it's the default, and my Windows Qt app happily uses vast and extensive exception handling with no problems. Qt MSVC builds use the /EHsc compiler option, which turns normal exception handling on.

Itemize answered 12/12, 2020 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.