Solved 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.
code:
int main(int argc, char *argv[])
{
try
{
throw new std::exception();
}
catch(std::exception &e)
{
// I expect this to occur but it doesn't.
qDebug()<< "Caught exception of type std::exception";
}
catch(...)
{
// This is occurring but I don't expect it.
qDebug()<< "Caught unknown exception";
}return 0;
}
Pro:
TEMPLATE = appQT += qml quick
CONFIG += c++11#added flags to enable exceptions and RTTI
QMAKE_CXXFLAGS += -fexceptions -frttiSOURCES += main.cpp
RESOURCES += qml.qrc
#Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =#Default rules for deployment.
include(deployment.pri)The qmake line is:
qmake.exe C:\HelloWorld\HelloWorld.pro -r -spec android-g++ "CONFIG+=debug" "CONFIG+=declarative_debug" "CONFIG+=qml_debug" -
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 valuethrow std::exception
(no use of new).
Note that if you throw exceptions allocated on the heap, you need to delete them as well. -
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.