Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Exception handling not working as expected on Android
Forum Updated to NodeBB v4.3 + New Features

Exception handling not working as expected on Android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
3 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Miller791
    wrote on last edited by
    #1

    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 = 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
    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"

    T 1 Reply Last reply
    0
    • M Miller791

      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 = 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
      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"

      T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      @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.

      M 1 Reply Last reply
      0
      • T t3685

        @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.

        M Offline
        M Offline
        Miller791
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved