Nominate our 2022 Qt Champions!

Exception handling not working as expected on Android

  • I have noticed C++ exception handling is not working as expected in my App on Android. I can reproduce the same issue in a simply Qt sample. Throwing exceptions like "std::exception" are not caught by their block but I can catch primitives like int. I thought it was caused by missing RTTI but adding "-frtti" to the CXX flags didn't change the behavior. I am guessing there is an issue with compiler flags. See the example below of throwing an std::exception() and the catch does not occur.

    This is with Qt 5.5, Android NDK r10, Device is ARM running Android 4.4.

    int main(int argc, char *argv[])
    throw new std::exception();
    catch(std::exception &e)
    // I expect this to occur but it doesn't.
    qDebug()<< "Caught exception of type std::exception";
    // This is occurring but I don't expect it.
    qDebug()<< "Caught unknown exception";

    return 0;


    TEMPLATE = app

    QT += qml quick
    CONFIG += c++11

    #added flags to enable exceptions and RTTI
    QMAKE_CXXFLAGS += -fexceptions -frtti

    SOURCES += main.cpp

    RESOURCES += qml.qrc

    #Additional import path used to resolve QML modules in Qt Creator's code model

    #Default rules for deployment.

    The qmake line is:
    qmake.exe C:\HelloWorld\ -r -spec android-g++ "CONFIG+=debug" "CONFIG+=declarative_debug" "CONFIG+=qml_debug"

  • @Miller791

    You are throwing a pointer to an exception new std::exception(). You are catching a reference to an exception which only triggers when throwing exception by value throw std::exception (no use of new).
    Note that if you throw exceptions allocated on the heap, you need to delete them as well.

  • @t3685

    Thanks, that was a typo on my part when I over simplified the problem from the original app. I suspect the original app issue is still an issue related to RTTI or other compiler flags and I will keep digging into in. The original app is a mix of SO's and I suspect one or more was not built with the right compiler options. Thanks again.

Log in to reply