Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception handling doesn't work with Qt on Windows

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?

like image 473
revers Avatar asked Nov 10 '10 21:11

revers


People also ask

How do you handle exceptions in Qt?

Currently, the only supported use case for recovering from exceptions thrown within Qt (for example due to out of memory) is to exit the event loop and do some cleanup before exiting the application. Typical use case: QApplication app(argc, argv); ... int ret; try { ret = app.

What are exceptions C++?

An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. Exceptions provide a way to transfer control from one part of a program to another.

What is a try catch?

The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions. When an exception is thrown, the common language runtime (CLR) looks for the catch statement that handles this exception.


1 Answers

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.

like image 190
revers Avatar answered Oct 20 '22 17:10

revers