Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Global exception handling with c++/QML
Forum Updated to NodeBB v4.3 + New Features

Global exception handling with c++/QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 5.3k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    PRod
    wrote on last edited by
    #1

    Hello. My task is to create an unhandled exception catching mechanism in an application with Qml UI and heavy c++ business logic. I am registering
    types using qmlRegisterType<> and create instances in Qml layer;

        MyApplication app(argc, argv);
        QQmlApplicationEngine *engine = new QQmlApplicationEngine();
        qmlRegisterType<presentation::uimodels::LoginForm>("Models", 0, 1, "LoginFormModel");
        engine->load(QUrl("qrc:/qml/views/LoginForm.qml"));
        return app.exec();
    
    Item {
        Component.onCompleted:
        {
            model.login();
        }
    
        LoginFormModel
        {
    	id: model
        }
    }
    

    I have a global exception handler, catching std::exceptions in overriden QGuiApplication::notify function and also a POSIX signal handler, in case that something bad like access violation errors happened.
    The problem is that unhandled exceptions thrown in my login() function did not get caught anywhere. Application just crashes unexpectedly without doing any post-exception handling. Is there a proper way of handling c++ exceptions with Qml or am i doing something wrong here. Please need advise.

    1 Reply Last reply
    1
    • S Offline
      S Offline
      stevemcgf
      wrote on last edited by
      #2

      Your C++ class should handle all exceptions. The invokable methods may return Status or have a Status member like QQmlComponent. So, you should check for those values in order to detect any error.

      1 Reply Last reply
      1
      • timdayT Offline
        timdayT Offline
        timday
        wrote on last edited by
        #3

        Yes, what @stevemcgf said. C++ exceptions are basically just another C++ flow-control construct but don't have a lot of meaning outside of the C++ domain. You might as well expect to be able to goto some QML from C++. And Qt has a long enough history it predates the fashion to use exceptions as a core error handling mechanism (like you find in the Java and C# worlds); http://doc.qt.io/qt-5/exceptionsafety.html is worth a read.

        (Brings back memories of years ago having to wrap exception-happy C++ as Microsoft COM componentry (where the standard was to use HRESULT return codes for all error handling). There was just no good alternative but to try { } catch (std::exception& e) in every method exposed to COM. Actually it was worse than that because some code was throwing types not derived from std::exception, but that's another story.)

        1 Reply Last reply
        1
        • P Offline
          P Offline
          PRod
          wrote on last edited by
          #4

          Thank you for your replies. Unfortunately by handling everything manually and fully rely on it means that we can give up on top-level handlers completely because there is not much point in them that way. That approach feels dangerous and unreliable, we would still like to have some mechanism on client side that would at least gather some logs and send them to us in case that something went terribly wrong.
          P.S. By reading forums and experimenting a bit i have found out that throwing exceptions through Qml layer is still possible in debug mode with Qml debugger on, so i will try to dig in this direction.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            PRod
            wrote on last edited by
            #5

            Update. I have found a hacky solution to my problem. Surprisingly, wrapping my model.login() call in try/catch block does the magic. Exceptions start to propagate through Qml and get caught in global handlers. This solution is still far from optimal, that's definetely not a good design to write multiple try/catches in UI layer. Also, try/catch mechanism is quite buggy in the moment. Still looking for a better solution.

            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